KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > shared > Integer_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
// Integer_Field
15
// YG 08.08.2001
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.JahiaField;
29 import org.jahia.engines.EngineMessages;
30 import org.jahia.engines.EngineParams;
31 import org.jahia.engines.JahiaEngine;
32 import org.jahia.engines.JahiaEngineTools;
33 import org.jahia.exceptions.JahiaException;
34 import org.jahia.params.ParamBean;
35 import org.jahia.registries.EnginesRegistry;
36 import org.jahia.registries.ServicesRegistry;
37 import org.jahia.services.acl.JahiaBaseACL;
38 import org.jahia.utils.JahiaTools;
39
40 public class Integer_Field
41 {
42
43     private static Logger logger = Logger.getLogger(Integer_Field.class);
44
45     private static Integer_Field theObject = null;
46     private static final String JavaDoc JSP_FILE = "/jsp/jahia/engines/shared/integer_field.jsp";
47     public static final String JavaDoc READONLY_JSP = "/jsp/jahia/engines/shared/readonly_smalltext_field.jsp";
48
49
50     /***
51         * getInstance
52         * AK 19.12.2000
53         *
54         */

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

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

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

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

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

132
133
134     /***
135         * saves data in datasource
136         *
137         * @param jParams a ParamBean object
138         * @param engineMap the engine hashmap
139         * @param theField the field we are working on
140         * @return true if everything went okay, false if not
141         *
142         */

143     private boolean saveData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
144     throws JahiaException
145     {
146         logger.debug("Saving Field " + theField.getDefinition().getName() + " : " + theField.getValue() );
147         return theField.save(jParams);
148     } // end saveData
149

150
151
152     /***
153         * composes engine hash map
154         *
155         * @param jParams a ParamBean object
156         * @param engineMap the engine hashmap
157         * @param theField the field we are working on
158         * @return true if everything went okay, false if not
159         *
160         */

161     private boolean composeEngineMap( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
162     throws JahiaException
163     {
164
165         boolean editable = false;
166         JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer");
167
168         if ( theContainer == null ){
169             // in case of a field , not a field in a container
170
editable = true;
171         } else {
172             HashMap JavaDoc ctnListFieldAcls = (HashMap JavaDoc) engineMap
173                     .get("ctnListFieldAcls");
174
175             if (theContainer.getListID() != 0 && ctnListFieldAcls != null
176                     && ctnListFieldAcls.size() > 0) {
177                 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL(
178                         ctnListFieldAcls, theField.getID());
179                 if (acl != null) {
180                     editable = acl.getPermission(jParams.getUser(),
181                             JahiaBaseACL.WRITE_RIGHTS, true);
182                 }
183             } else {
184                 editable = true;
185             }
186         }
187         
188         String JavaDoc output = "";
189
190         if ( editable ) {
191
192             String JavaDoc localSwitchUrl = "ReloadEngine('localswitch" + EngineParams.VALUE_TOKEN + "yes')";
193             engineMap.put( "localSwitchUrl", localSwitchUrl );
194
195             String JavaDoc dataSourceConnectUrl = "";
196             JahiaEngine dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "selectdatasource" );
197             dataSourceConnectUrl = dsEngine.renderLink( jParams, theField );
198             engineMap.put( "dataSourceConnectUrl", dataSourceConnectUrl );
199
200             String JavaDoc dataSourceIDUrl = "";
201             dsEngine = (JahiaEngine) EnginesRegistry.getInstance().getEngine( "viewdatasourceid" );
202             dataSourceIDUrl = dsEngine.renderLink( jParams, theField );
203             engineMap.put( "dataSourceIDUrl", dataSourceIDUrl );
204
205             boolean isIE = false;
206             String JavaDoc userAgent = jParams.getRequest().getHeader( "user-agent" );
207             if (userAgent != null) {
208                 isIE = (userAgent.indexOf( "IE" ) != -1);
209             }
210             jParams.getRequest().setAttribute("isIE" , new Boolean JavaDoc(isIE));
211
212             theField.setValue( FormDataManager.getInstance().formEncode(theField.getValue()) );
213             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, JSP_FILE );
214         } else {
215             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, READONLY_JSP );
216         }
217
218         engineMap.put( "fieldForm", output );
219
220         return true;
221     } // end composeEngineMap
222

223
224 } // end SmallText_Field
225
Popular Tags