KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > shared > Undefined_Field


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 // EV 14.01.20001
14

15 package org.jahia.engines.shared;
16
17 import java.util.Enumeration JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.jahia.data.FormDataManager;
24 import org.jahia.data.containers.JahiaContainer;
25 import org.jahia.data.fields.FieldTypes;
26 import org.jahia.data.fields.JahiaField;
27 import org.jahia.data.fields.LoadFlags;
28 import org.jahia.engines.JahiaEngine;
29 import org.jahia.engines.JahiaEngineTools;
30 import org.jahia.exceptions.JahiaException;
31 import org.jahia.params.ParamBean;
32 import org.jahia.registries.ServicesRegistry;
33 import org.jahia.services.acl.JahiaBaseACL;
34 import org.jahia.services.pages.JahiaPage;
35
36 public class Undefined_Field {
37
38     private static Logger logger = Logger.getLogger(Undefined_Field.class);
39
40     private static Undefined_Field theObject = null;
41     private static final String JavaDoc JSP_FILE =
42         "/jsp/jahia/engines/shared/undefined_field.jsp";
43
44     /***
45      * getInstance
46      * AK 19.12.2000
47      *
48      */

49     public static synchronized Undefined_Field getInstance () {
50         if (theObject == null) {
51             theObject = new Undefined_Field();
52         }
53         return theObject;
54     } // end getInstance
55

56     /***
57      * handles the field actions
58      *
59      * @param jParams a ParamBean object
60      * @param mode the mode, according to JahiaEngine
61      * @return true if everything went okay, false if not
62      * @see org.jahia.engines.JahiaEngine
63      *
64      */

65     public boolean handleField (ParamBean jParams, Integer JavaDoc modeInt,
66                                 HashMap JavaDoc engineMap)
67         throws JahiaException {
68         int mode = modeInt.intValue();
69         JahiaField theField = (JahiaField) engineMap.get("theField");
70         switch (mode) {
71             case (JahiaEngine.LOAD_MODE):
72                 return composeEngineMap(jParams, engineMap, theField);
73             case (JahiaEngine.UPDATE_MODE):
74                 return getFormData(jParams, engineMap, theField);
75             case (JahiaEngine.SAVE_MODE):
76                 return saveData(jParams, engineMap, theField);
77         }
78         return false;
79     } // end handleField
80

81     /***
82      * gets POST data from the form and saves it in session
83      *
84      * @param jParams a ParamBean object
85      * @param engineMap the engine hashmap
86      * @param theField the field we are working on
87      * @return true if everything went okay, false if not
88      *
89      */

90     private boolean getFormData (ParamBean jParams, HashMap JavaDoc engineMap,
91                                  JahiaField theField)
92         throws JahiaException {
93         boolean out = true;
94         String JavaDoc choosedType = "";
95
96         String JavaDoc fieldTypeStr = jParams.getRequest().getParameter("_" +
97             new Integer JavaDoc(theField.getID()).toString());
98         choosedType += jParams.getRequest().getParameter("choosedType");
99
100         logger.debug("fieldType: " + fieldTypeStr);
101         if (choosedType.equals("yes") && (fieldTypeStr != null)) {
102             try {
103                 int fieldType = Integer.parseInt(fieldTypeStr);
104                 JahiaField f = ServicesRegistry.getInstance().
105                                getJahiaFieldService().
106                                createJahiaField(theField.getID(),
107                                                 theField.getJahiaID(),
108                                                 theField.getPageID(),
109                                                 theField.getctnid(),
110                                                 theField.getFieldDefID(),
111                                                 fieldType,
112                                                 theField.getConnectType(),
113                                                 theField.getValue(),
114                                                 theField.getRank(),
115                                                 theField.getAclID(),
116                                                 theField.getVersionID(),
117                                                 theField.getWorkflowState(),
118                                                 theField.getLanguageCode());
119
120                 String JavaDoc flag = (String JavaDoc) engineMap.get("updateField");
121                 if (flag == null) {
122                     JahiaContainer theContainer = (JahiaContainer) engineMap.
123                                                   get("theContainer");
124                     theContainer.setField(f);
125                     engineMap.put("theContainer", theContainer);
126                 }
127
128                 //JahiaFieldDefinition fDef = JahiaFieldDefinitionsRegistry.
129
//getInstance().
130
//getDefinition(theField.getFieldDefID());
131
//fDef.setType( fieldType, fDef.getID() );
132
//JahiaFieldDefinitionsRegistry.getInstance().
133
// setDefinition( fDef );
134

135                 theField = f;
136                 // theField.setType( fieldType );
137
engineMap.put("theField", f);
138             } catch (NumberFormatException JavaDoc nfe) {
139                 theField.setType(0);
140                 out = false;
141             }
142         }
143         if (!choosedType.equals("yes")) {
144             theField.setType(0);
145         }
146         return out;
147     } // end getFormData
148

149     /***
150      * saves data in datasource
151      *
152      * @param jParams a ParamBean object
153      * @param engineMap the engine hashmap
154      * @param theField the field we are working on
155      * @return true if everything went okay, false if not
156      *
157      */

158     private boolean saveData (ParamBean jParams, HashMap JavaDoc engineMap,
159                               JahiaField theField)
160         throws JahiaException {
161         return theField.save(jParams);
162     } // end saveData
163

164     /***
165      * composes engine hash map
166      *
167      * @param jParams a ParamBean object
168      * @param engineMap the engine hashmap
169      * @param theField the field we are working on
170      * @return true if everything went okay, false if not
171      *
172      */

173     private boolean composeEngineMap (ParamBean jParams, HashMap JavaDoc engineMap,
174                                       JahiaField theField)
175         throws JahiaException {
176
177         boolean editable = false;
178         JahiaContainer theContainer = (JahiaContainer) engineMap.get(
179             "theContainer");
180         if (theContainer == null) {
181             // in case of a field , not a field in a container
182
editable = true;
183         } else {
184             HashMap JavaDoc ctnListFieldAcls = (HashMap JavaDoc) engineMap
185                     .get("ctnListFieldAcls");
186
187             if (theContainer.getListID() != 0 && ctnListFieldAcls != null
188                     && ctnListFieldAcls.size() > 0) {
189                 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL(
190                         ctnListFieldAcls, theField.getID());
191                 if (acl != null) {
192                     editable = acl.getPermission(jParams.getUser(),
193                             JahiaBaseACL.WRITE_RIGHTS, true);
194                 }
195             } else {
196                 editable = true;
197             }
198         }
199
200         String JavaDoc output = "";
201
202         if (editable) {
203
204             // soon obsolete
205
Hashtable JavaDoc typesList = FieldTypes.getInstance().getFieldTypes();
206             Hashtable JavaDoc typesList2 = new Hashtable JavaDoc();
207             // check which types must be inserted in the undefined field list
208
int pageID = theField.getPageID();
209             JahiaPage thePage = ServicesRegistry.getInstance().
210                                 getJahiaPageService().lookupPage(pageID,
211                 jParams);
212             String JavaDoc defValue = theField.getDefinition().getDefaultValue(thePage.
213                 getPageTemplateID());
214
215             try {
216                 if (defValue.toUpperCase().indexOf("JAHIA_MASKTYPE") != -1) {
217                     defValue = defValue.substring(defValue.indexOf("[") + 1,
218                                                   defValue.indexOf("]"));
219                     //logger.debug(defValue);
220
}
221                 int defValueInt = Integer.parseInt(defValue);
222                 if (defValueInt == -1) {
223                     typesList2 = typesList;
224                 } else {
225                     Enumeration JavaDoc keys = typesList.keys();
226                     Hashtable JavaDoc loadFlags = LoadFlags.getInstance().getLoadFlags();
227                     int loadFlag = 0;
228                     int i = -1;
229                     while (keys.hasMoreElements()) {
230                         i++;
231                         String JavaDoc theKey = (String JavaDoc) keys.nextElement();
232                         loadFlag = ( (Integer JavaDoc) loadFlags.get( typesList.get(theKey)) != null)
233                                    ?
234                                    ( (Integer JavaDoc) loadFlags.get( typesList.
235                             get(theKey))).intValue() : 0;
236                         boolean flag = ( (defValueInt & loadFlag) == loadFlag) ? true : false;
237                         //logger.debug("* the Key:"+ theKey +" deValInt: "+defValueInt +" loadFlag: "+loadFlag +" d & l: "+f);
238
if (flag) {
239                             typesList2.put(theKey,
240                                            typesList.get(theKey));
241                         }
242                     }
243                 }
244
245             } catch (NumberFormatException JavaDoc e) {
246                 typesList2.put("SmallText", typesList.get("SmallText"));
247                 typesList2.put("BigText", typesList.get("BigText"));
248                 typesList2.put("Application",
249                                typesList.get("Application"));
250             }
251
252             engineMap.put("typesList", typesList2);
253
254             // This code is no longer used, but we might want to use the new
255
// categories in the future.
256
Vector JavaDoc categoryList = new Vector JavaDoc();
257             engineMap.put("categoryList", categoryList);
258
259             theField.setValue(FormDataManager.getInstance().formEncode(theField.
260                 getValue()));
261             output = ServicesRegistry.getInstance().getJahiaFetcherService().
262                      fetchServlet(jParams, JSP_FILE);
263         } else {
264             output = ServicesRegistry.getInstance().getJahiaFetcherService().
265                      fetchServlet(jParams, JahiaEngine.NOT_EDITABLE_FIELD);
266         }
267
268         engineMap.put("fieldForm", output);
269         return true;
270
271     } // end composeEngineMap
272

273 } // end SmallText
Popular Tags