KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > fields > JahiaApplicationField


1 // JahiaApplicationField
2
// YG 17.07.2001
3

4 package org.jahia.data.fields;
5
6 import java.util.Properties JavaDoc;
7
8 import org.jahia.data.ConnectionTypes;
9 import org.jahia.data.FormDataManager;
10 import org.jahia.data.applications.ApplicationBean;
11 import org.jahia.exceptions.JahiaException;
12 import org.jahia.params.ParamBean;
13 import org.jahia.registries.ServicesRegistry;
14 import org.jahia.services.fields.ContentApplicationField;
15 import org.jahia.services.fields.ContentField;
16 import org.jahia.services.version.EntrySaveRequest;
17 import org.jahia.utils.JahiaTools;
18
19 public class JahiaApplicationField extends JahiaField implements JahiaAllowApplyChangeToAllLangField
20 {
21
22     private static org.apache.log4j.Logger logger =
23             org.apache.log4j.Logger.getLogger(JahiaApplicationField.class);
24
25         /***
26         * constructor
27         * YG 17.07.2001
28         *
29         */

30     public JahiaApplicationField(Integer JavaDoc ID,
31                             Integer JavaDoc jahiaID,
32                             Integer JavaDoc pageID,
33                             Integer JavaDoc ctnid,
34                             Integer JavaDoc fieldDefID,
35                             Integer JavaDoc fieldType,
36                             Integer JavaDoc connectType,
37                             String JavaDoc fieldValue,
38                             Integer JavaDoc rank,
39                             Integer JavaDoc aclID,
40                             Integer JavaDoc versionID,
41                             Integer JavaDoc versionStatus,
42                             String JavaDoc languageCode)
43     {
44         super(ID, jahiaID, pageID, ctnid, fieldDefID, fieldType, connectType,
45               fieldValue, rank, aclID, versionID, versionStatus, languageCode);
46
47         if ( isShared() ){
48             this.languageCode = ContentField.SHARED_LANGUAGE;
49         }
50
51     } // end constructor
52

53
54     public void load(int loadFlag, ParamBean jParams)
55     throws JahiaException
56     {
57         this.setObject(this.getValue()); // to save the app id... used in engines
58

59         try {
60             logger.debug("Loading application field...");
61
62             ContentApplicationField contentApplicationField = (ContentApplicationField)ContentApplicationField.getField(getID());
63             logger.debug("value=" + getValue());
64
65             String JavaDoc val = contentApplicationField.getValue(jParams);
66
67             this.setValue(JahiaTools.replacePattern(FormDataManager.getInstance().htmlEncode(val),"@","@"));
68
69         } catch ( Throwable JavaDoc t ){
70             logger.error("Problem loading application field value for field " + getID(), t);
71             this.setValue("Exception loading application data :\n" + t.getMessage());
72         }
73
74         /*
75         if ((loadFlag & LoadFlags.APPLICATION) != 0)
76         {
77             switch (this.getConnectType())
78             {
79                 case (ConnectionTypes.LOCAL) :
80                     this.setObject(this.getValue()); // to save the app id... used in engines
81                     ServicesRegistry tmpServicesRegistry = ServicesRegistry.getInstance();
82
83                     System.out.println("ID: "+this.getID()+" Value: "+this.getValue());
84                     String tmpValue = "";
85                     if (jParams != null)
86                     {
87                         tmpValue = ServicesRegistry.getInstance().
88                         getJahiaApplicationsDispatchingService().
89                         getAppOutput( this.getID(), this.getValue(), jParams);
90                     }
91                     try {
92                         tmpValue = (new RE("</?html>",RE.MATCH_CASEINDEPENDENT)).
93                             subst(tmpValue,"");
94                     } catch (RESyntaxException e) {
95                         JahiaConsole.println("JahiaFieldBaseService",e.toString());
96                     }
97
98                     try {
99                         this.setValue(FormDataManager.getInstance().
100                         decode(new StringBuffer().append("<html>").
101                         append(tmpValue).append("</html>").toString()));
102                     } catch ( Throwable t ){
103                         t.printStackTrace();
104                         this.setValue(tmpValue);
105                     }
106
107                     break;
108                 case (ConnectionTypes.DATASOURCE) :
109                 if ((loadFlag & LoadFlags.DATASOURCE) != 0) {
110                     this.setValue( JahiaDataSourceManager.getInstance().
111                     fields().getInstance().getRemoteFieldValue(
112                     this.getValue() ));
113                 }
114                 break;
115             }
116         }
117         */

118     }
119
120     public boolean save(ParamBean jParams)
121     throws JahiaException
122     {
123         switch (this.getConnectType())
124         {
125         case (ConnectionTypes.LOCAL) :
126
127             if ( this.getValue() != null && !this.getValue().equals("<empty>") ){
128                 int appID = Integer.parseInt(this.getValue());
129                 ApplicationBean app = ServicesRegistry.getInstance()
130                                 .getJahiaApplicationsManagerService()
131                                 .getApplication(appID);
132
133                 if ( app != null ){
134
135                     // check if the app has changed
136
String JavaDoc oldAppID = (String JavaDoc)this.getObject();
137                     if ( (oldAppID!=null) && !oldAppID.equals(this.getValue()) ){
138
139                         // App has changed, so delete old groups
140
ApplicationBean oldApp = ServicesRegistry
141                             .getInstance()
142                             .getJahiaApplicationsManagerService()
143                             .getApplication(Integer.parseInt(oldAppID));
144
145                         ServicesRegistry.getInstance()
146                             .getJahiaApplicationsManagerService()
147                             .deleteApplicationGroups(oldApp,this);
148                     }
149
150                     // Create new groups on this field ( only if not exists )
151
ServicesRegistry.getInstance()
152                         .getJahiaApplicationsManagerService()
153                         .createApplicationGroups(app,this);
154                 }
155                 EntrySaveRequest saveRequest = new EntrySaveRequest(jParams.getUser(),this.getLanguageCode());
156                 ContentApplicationField contentField = (ContentApplicationField)ContentField.getField(this.getID());
157                 contentField.setAppID(appID,saveRequest);
158                 break;
159             }
160         }
161         return true;
162     }
163
164     public void delete(ParamBean jParams)
165     throws JahiaException
166     {
167         switch (this.getConnectType())
168         {
169         case (ConnectionTypes.LOCAL) :
170             /* Versioning issue :
171             // We don't want to delete the appication roles anymore ( versioning ).
172             if ( this.getValue() != null ){
173                 try {
174                     int appID = Integer.parseInt(this.getValue());
175                     ApplicationBean app = ServicesRegistry.getInstance().
176                         getJahiaApplicationsManagerService().
177                         getApplication(appID);
178                     if ( app != null ){
179                         ServicesRegistry.getInstance().
180                             getJahiaApplicationsManagerService().
181                             deleteApplicationGroups(app,this);
182                     }
183                 } catch (NumberFormatException nfe){
184                     //
185                 }
186
187             }*/

188             break;
189         }
190     }
191
192     public String JavaDoc getEngineName()
193     {
194         return "org.jahia.engines.shared.Application_Field";
195     }
196
197     public String JavaDoc getFieldContent4Ranking()
198     throws JahiaException
199     {
200         String JavaDoc fieldInfo = "";
201         String JavaDoc appIDStr = (String JavaDoc)this.getObject();
202         int appID = 0;
203         if (appIDStr != null)
204         {
205             try {
206                     appID = Integer.parseInt(appIDStr);
207                 } catch (NumberFormatException JavaDoc nfe) {
208                     ;
209                 }
210         }
211
212         ApplicationBean app = ServicesRegistry.getInstance().
213                               getJahiaApplicationsManagerService().
214                               getApplication(appID);
215
216         if (app != null)
217         {
218             fieldInfo = app.getName();
219         }
220         else
221         {
222             fieldInfo = this.getValue();
223         }
224         return fieldInfo;
225     }
226
227     public String JavaDoc getIconNameOff()
228     {
229         return "application";
230     }
231
232     public String JavaDoc getIconNameOn()
233     {
234         return "application_on";
235     }
236
237     public JahiaField cloneField(int newctnid, int newPageID, int clonedAclID, boolean childrenCloned)
238     throws JahiaException
239     {
240         JahiaField clonedField = ServicesRegistry.getInstance().getJahiaFieldService().
241                                  createJahiaField(0, this.getJahiaID(),
242                                  newPageID, newctnid,
243                                  this.getFieldDefID(), this.getType(),
244                                  this.getConnectType(),
245                                  (String JavaDoc)this.getObject(), this.getRank(),
246                                  clonedAclID, this.getVersionID(), this.getWorkflowState(),
247                                  this.getLanguageCode());
248
249         //toDebug("cloneField(): value = "+this.getValue());
250
if (clonedField == null)
251         {
252             throw new JahiaException ("Could not clone field.",
253                 "Could not instanciate a new JahiaField object.",
254                 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY);
255         }
256
257         clonedField.setRawValue(this.getRawValue());
258         clonedField.setObject( this.getObject() );
259         clonedField.setProperties( (Properties JavaDoc)(this.getProperties()).clone() );
260
261         return clonedField;
262     }
263
264     public Object JavaDoc clone()
265     {
266         Object JavaDoc objItem = this.getObject();
267         JahiaApplicationField app = new JahiaApplicationField (new Integer JavaDoc(ID), new Integer JavaDoc(jahiaID),
268                                         new Integer JavaDoc(pageID),
269                                         new Integer JavaDoc(ctnid),
270                                         new Integer JavaDoc(fieldDefID),
271                                         new Integer JavaDoc(fieldType),
272                                         new Integer JavaDoc(connectType),
273                                         fieldValue, new Integer JavaDoc(rank),
274                                         new Integer JavaDoc(aclID),
275                                         new Integer JavaDoc(versionID),
276                                         new Integer JavaDoc(workflowState),
277                                         new String JavaDoc(getLanguageCode()) );
278         app.setObject(objItem);
279         return app;
280     }
281
282     /**
283      * Is this kind of field shared (i.e. not one version for each language, but one version for every language)
284      */

285     public boolean isShared (){
286         return true;
287     }
288
289     /**
290      * Copy the internal value of current language to another language.
291      * Must be implemented by conctrete field for specific implementation.
292      *
293      * @param aField A same field in another language
294      */

295     public void copyValueInAnotherLanguage (JahiaField aField,ParamBean jParams)
296     throws JahiaException {
297         if ( aField == null ){
298             return;
299         }
300         aField.setValue(this.getValue());
301         aField.setRawValue(this.getValue());
302         aField.setObject(this.getObject());
303     }
304
305 }
306
Popular Tags