KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > shared > Date_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 //
14
// Date_Field
15
// EV 14.01.20001
16
//
17
// handleField( mode, jParams )
18
//
19

20 package org.jahia.engines.shared;
21
22 import java.util.HashMap JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.jahia.data.ConnectionTypes;
26 import org.jahia.data.FormDataManager;
27 import org.jahia.data.containers.JahiaContainer;
28 import org.jahia.data.fields.JahiaFieldDefinitionProperties;
29 import org.jahia.data.fields.JahiaField;
30 import org.jahia.engines.EngineMessages;
31 import org.jahia.engines.EngineParams;
32 import org.jahia.engines.JahiaEngine;
33 import org.jahia.engines.JahiaEngineTools;
34 import org.jahia.exceptions.JahiaException;
35 import org.jahia.params.ParamBean;
36 import org.jahia.registries.EnginesRegistry;
37 import org.jahia.registries.ServicesRegistry;
38 import org.jahia.services.acl.JahiaBaseACL;
39 import org.jahia.utils.JahiaTools;
40
41 public class Date_Field
42 {
43
44     private static Logger logger = Logger.getLogger(Date_Field.class);
45
46     private static Date_Field theObject = null;
47     public static final String JavaDoc JSP_FILE = "/jsp/jahia/engines/shared/date_field.jsp";
48     public static final String JavaDoc READONLY_JSP = "/jsp/jahia/engines/shared/readonly_date_field.jsp";
49
50
51     /***
52         * getInstance
53         * AK 19.12.2000
54         *
55         */

56     public static synchronized Date_Field getInstance()
57     {
58         if (theObject == null) {
59             theObject = new Date_Field();
60         }
61         return theObject;
62     } // end getInstance
63

64
65
66     /***
67         * handles the field actions
68         *
69         * @param jParams a ParamBean object
70         * @param mode the mode, according to JahiaEngine
71         * @return true if everything went okay, false if not
72         * @see org.jahia.engines.JahiaEngine
73         *
74         */

75     public boolean handleField( ParamBean jParams, Integer JavaDoc modeInt, HashMap JavaDoc engineMap )
76     throws JahiaException
77     {
78         int mode = modeInt.intValue();
79         JahiaField theField = (JahiaField) engineMap.get( "theField" );
80         switch (mode)
81         {
82             case (JahiaEngine.LOAD_MODE) : return composeEngineMap( jParams, engineMap, theField );
83             case (JahiaEngine.UPDATE_MODE) : return getFormData( jParams, engineMap, theField );
84             case (JahiaEngine.SAVE_MODE) : return saveData( jParams, engineMap, theField );
85         }
86         return false;
87     } // end handleField
88

89
90
91
92     /***
93         * gets POST data from the form and saves it in session
94         *
95         * @param jParams a ParamBean object
96         * @param engineMap the engine hashmap
97         * @param theField the field we are working on
98         * @return true if everything went okay, false if not
99         *
100         */

101     private boolean getFormData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
102     throws JahiaException
103     {
104         boolean out = true;
105
106         String JavaDoc fieldValue = jParams.getRequest().getParameter( "_" + new Integer JavaDoc(theField.getID()).toString() );
107         String JavaDoc fieldValueStr = jParams.getRequest().getParameter( "_Str" + new Integer JavaDoc(theField.getID()).toString() );
108         String JavaDoc engineParams = jParams.getRequest().getParameter( "engine_params" );
109         fieldValue = JahiaTools.replacePattern(fieldValue,"|","|");
110
111
112
113         EngineParams eParams = new EngineParams( engineParams );
114         String JavaDoc localSwitch = eParams.getParameter( "localswitch" );
115         String JavaDoc dataSourceUrl = eParams.getParameter( "dsurl" );
116
117         if (dataSourceUrl != null) {
118             theField.setValue( dataSourceUrl );
119             theField.setConnectType( ConnectionTypes.DATASOURCE );
120         } else if (localSwitch != null) {
121             theField.setValue( theField.getDefinition().getDefaultValue( jParams.getPage().getPageTemplateID() ) );
122             theField.setConnectType( ConnectionTypes.LOCAL );
123         } else if (fieldValue != null) {
124             EngineMessages resultMessages = new EngineMessages();
125             if (FormDataManager.getInstance().checkIntegrity(fieldValue, resultMessages)) {
126                 theField.setObject( fieldValue );
127                 theField.setValue( fieldValueStr );
128             } else {
129                 theField.setObject( fieldValue );
130                 theField.setValue( fieldValueStr );
131                 out = false;
132             }
133             //System.out.println("%%%%%%%%%%% getForm - Object: "+fieldValue);
134
//System.out.println("%%%%%%%%%%% getForm - Value: "+fieldValueStr);
135
}
136
137         return out;
138     } // end getFormData
139

140
141
142     /***
143         * saves data in datasource
144         *
145         * @param jParams a ParamBean object
146         * @param engineMap the engine hashmap
147         * @param theField the field we are working on
148         * @return true if everything went okay, false if not
149         *
150         */

151     private boolean saveData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
152     throws JahiaException
153     {
154         logger.debug("Saving Field " + theField.getDefinition().getName() + " : " + theField.getValue() );
155         return theField.save(jParams);
156     } // end saveData
157

158
159
160     /***
161         * composes engine hash map
162         *
163         * @param jParams a ParamBean object
164         * @param engineMap the engine hashmap
165         * @param theField the field we are working on
166         * @return true if everything went okay, false if not
167         *
168         */

169     private boolean composeEngineMap( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
170     throws JahiaException
171     {
172         boolean editable = false;
173         JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer");
174         if ( theContainer == null ){
175             // in case of a field , not a field in a container
176
editable = true;
177         } else {
178             HashMap JavaDoc ctnListFieldAcls = (HashMap JavaDoc) engineMap
179                     .get("ctnListFieldAcls");
180
181             if (theContainer.getListID() != 0 && ctnListFieldAcls != null
182                     && ctnListFieldAcls.size() > 0) {
183                 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL(
184                         ctnListFieldAcls, theField.getID());
185                 if (acl != null) {
186                     editable = acl.getPermission(jParams.getUser(),
187                             JahiaBaseACL.WRITE_RIGHTS, true);
188                 }
189             } else {
190                 editable = true;
191             }
192         }
193
194         String JavaDoc output = "";
195
196         String JavaDoc forward = theField.getDefinition()
197                        .getProperty(JahiaFieldDefinitionProperties.FIELD_UPDATE_JSP_FILE_PROP);
198         if ( forward == null ){
199             forward = Date_Field.JSP_FILE;
200             if ( !editable ){
201                 forward = Date_Field.READONLY_JSP;
202             }
203         }
204
205         if ( editable ){
206
207             String JavaDoc localSwitchUrl = "ReloadEngine('localswitch" + EngineParams.VALUE_TOKEN + "yes')";
208             engineMap.put( "localSwitchUrl", localSwitchUrl );
209
210             String JavaDoc dataSourceConnectUrl = "";
211             JahiaEngine dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "selectdatasource" );
212             dataSourceConnectUrl = dsEngine.renderLink( jParams, theField );
213             engineMap.put( "dataSourceConnectUrl", dataSourceConnectUrl );
214
215             String JavaDoc dataSourceIDUrl = "";
216             dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "viewdatasourceid" );
217             dataSourceIDUrl = dsEngine.renderLink( jParams, theField );
218             engineMap.put( "dataSourceIDUrl", dataSourceIDUrl );
219
220             theField.setValue( FormDataManager.getInstance().formEncode(theField.getValue()) );
221             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward );
222             //System.out.println("output: "+output);
223
} else {
224             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, forward );
225         }
226
227         engineMap.put( "fieldForm", output );
228
229         return true;
230     } // end composeEngineMap
231

232
233 } // end SmallText_Field
234
Popular Tags