View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  package org.caleigo.core;
19  
20  import org.caleigo.security.*;
21  
22  /***
23   * @author Klas Ehnrot
24   *
25   * Dynamically adds the user properties to 
26   */
27  public class UserProxyData implements IProxyData
28  {
29  
30      private String propertyName;
31  
32      /***
33       * Parses the incoming value and converts it into a UserProxyData.
34       * Expecting a string formatted as : 
35       * #USER.propertyName   where #USER. is the static directive and
36       * propertyName is the name of the property to lookup in UserConfig.
37       *    
38       * @param value the data
39       * @return UserProxyData
40       */
41      public static UserProxyData parse(String value)
42      {
43          if (value.startsWith("#USER.") && value.indexOf('#', 1) != -1)
44          {
45              String propertyName =
46                  value.substring(6, value.indexOf('#', 1)).trim();
47              /* if ((propertyName.toLowerCase().equals("userid")) ||
48                   (UserInfo.getCurrentUserInfo().getProperty(propertyName) != null) ||
49                   (UserInfo.getCurrentUserInfo().getProperty("entity") != null && 
50                  ((IEntity) UserInfo.getCurrentUserInfo().getProperty("entity")).getData(propertyName) != null))
51               {*/
52              return new UserProxyData(propertyName);
53              //}
54          }
55  
56          return null;
57      }
58  
59      /***
60         * Used to convert an object to a Date Object, either the java.util.Date object or 
61         * a DateProxyData object.
62         * 
63         * @param data The data representing a Date
64         * @return Object The created data object
65         */
66      public static Object convertToStringObject(Object data)
67      {
68      
69          if (data instanceof UserProxyData)
70              return data;
71          else if (data != null)
72          {
73              data = data.toString();
74              UserProxyData userProxyData = UserProxyData.parse((String) data);
75              if (userProxyData != null)
76              {
77                  return userProxyData;
78              }
79              return data;
80          }
81          else
82              return null;
83      }
84  
85      public UserProxyData(String propertyName)
86      {
87          this.propertyName = propertyName;
88      }
89  
90      /***
91       * Implementation of the IProxy interface getData
92       * Goes through UserInfo and lookups the propertyName
93       * 
94       * @see org.caleigo.core.IProxyData#getData()
95       */
96      public Object getData()
97      {
98  
99          // Get the UserInfo
100         UserInfo userInfo = null;
101         try
102         {
103             userInfo = UserInfo.getCurrentUserInfo();
104 
105         }
106         catch (LoginCanceledException e)
107         {
108             return "#USER." + propertyName + "#";
109         }
110         catch (IllegalStateException e)
111         {
112             return "#USER." + propertyName + "#";
113         }
114 
115         // See if we want the User Id first
116         if (propertyName.toLowerCase().equals("userid"))
117         {
118             return userInfo.getUserID();
119         }
120 
121         // See if we find the property
122         Object data = userInfo.getProperty(this.propertyName);
123         if (data != null)
124         {
125             return data.toString();
126         }
127 
128         // See if there is an Entity property on the UserInfo. If so, 
129         // try to get the value of the field with codename = propertyName
130         data = userInfo.getProperty("entity");
131         if (data != null)
132         {
133             IEntity entity = (IEntity) data;
134             data = entity.getData(this.propertyName);
135             if (data != null)
136                 return data.toString();
137         }
138 
139         return "#USER." + propertyName + "#";
140     }
141 
142     /*** 
143      * Implementation of the IProxy Interface getDataType
144      * 
145      * @return Returns a DataType.STRING
146      * @see org.caleigo.core.IProxyData#getDataType()
147      */
148     public DataType getDataType()
149     {
150         return DataType.STRING;
151     }
152 
153 }