KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > form > eventhandler > CTUtils


1 package de.webman.form.eventhandler;
2
3 import java.util.*;
4 import java.sql.*;
5 import java.io.*;
6 import java.net.*;
7 import com.teamkonzept.web.*;
8 import com.teamkonzept.lib.*;
9 import com.teamkonzept.field.*;
10 import com.teamkonzept.field.db.TKFormDBData;
11 import com.teamkonzept.field.db.TKFormDBInterface;
12 import com.teamkonzept.db.*;
13 import com.teamkonzept.webman.mainint.*;
14 import com.teamkonzept.publishing.markups.*;
15 import com.teamkonzept.international.LanguageManager;
16 import com.teamkonzept.webman.*;
17 import com.teamkonzept.webman.db.*;
18 import com.teamkonzept.webman.mainint.*;
19 import com.teamkonzept.webman.mainint.events.*;
20 import com.teamkonzept.webman.mainint.db.*;
21 import com.teamkonzept.webman.mainint.db.queries.*;
22 import de.webman.form.Form;
23 import de.webman.form.db.FormConstants;
24 import de.webman.form.db.queries.FormExists;
25
26 /**
27  * Provides convenience methods used by content type event handlers.
28  *
29  * @author $Author: uli $
30  * @version $Revision: 1.11 $
31  */

32 public class CTUtils
33     implements CTParameters,
34                ParameterTypes,
35                DatabaseDefaults
36 {
37
38     /**
39      * The language manager context.
40      */

41     public static final String JavaDoc LANGUAGE_CONTEXT = "field";
42
43     /**
44      * The <CODE>form processing</CODE> property group.
45      */

46     public static final String JavaDoc PROPERTY_GROUP_FORM_PROCESSING = "FORM_PROCESSING";
47
48     /**
49      * The <CODE>enable sloppy naming</CODE> property.
50      */

51     public static final String JavaDoc PROPERTY_ENABLE_SLOPPY_NAMING = "ENABLE_SLOPPY_NAMING";
52
53     /**
54      * The <CODE>enable unsafe modification</CODE> property.
55      */

56     public static final String JavaDoc PROPERTY_ENABLE_UNSAFE_MODIFICATION = "ENABLE_UNSAFE_MODIFICATION";
57
58
59     /**
60      * Keeps the context by passing all parameters of the event
61      * into the given template.
62      *
63      * @param event the event.
64      * @param template the template.
65      */

66     public static void keepCTContext (TKEvent event, TKHTMLTemplate template)
67     {
68         template.set(FORM_TYPE, event.getNotNullParameter(PARAMETER, FORM_TYPE));
69         template.set(FORM_ID, event.getNotNullParameter(PARAMETER, FORM_ID));
70         template.set(ISTEST, event.getNotNullParameter(PARAMETER, ISTEST));
71         template.set(MODIFY_CONTEXT, event.getNotNullParameter(PARAMETER, MODIFY_CONTEXT));
72         template.set(TMP_FORM_ID, event.getNotNullParameter(PARAMETER, TMP_FORM_ID));
73         template.set(UPBASE, event.getParams().hasMultiple(PARAMETER, UPBASE)
74                                 ? event.getParams().get(PARAMETER, UPBASE, 0)
75                                 : event.getNotNullParameter(PARAMETER, UPBASE));
76     }
77
78     /**
79      * Switches between normal and testing mode by setting the
80      * according parameters in the template.
81      *
82      * @param template the template.
83      * @param test specifies the mode.
84      */

85     public static void switchTest (TKHTMLTemplate template, boolean test)
86     {
87         if (test)
88         {
89             template.set(ISTEST, "testing");
90             template.set(MODIFY_CONTEXT, CTTEST);
91         }
92         else
93         {
94             template.set(TMP_FORM_ID, EMPTY);
95             template.set(ISTEST, EMPTY);
96             template.set(MODIFY_CONTEXT, CT);
97         }
98     }
99
100     /**
101      * Creates the top-level field structure based on the given form type.
102      *
103      * @param type the form type.
104      * @return the top-level field structure based on the given form type.
105      * @throws TKException if an error occurred during structure creation.
106      */

107     public static TKBaseField getStructure (int type)
108         throws TKException
109     {
110         try
111         {
112             // Create simple selection with option check.
113
TKFieldSwitch allSwitch = new TKFieldSwitch(TKBaseField.SUB_TYPE_KEY, new TKVector(), LanguageManager.getText(LANGUAGE_CONTEXT, "CHOICE"), TKFieldSwitch.CHECK_OPTION);
114
115             // Create selection list.
116
TKFieldSwitchList allSwitchList = new TKFieldSwitchList(TKBaseField.SUB_LIST_KEY, new TKVector(), LanguageManager.getText(LANGUAGE_CONTEXT, "CHOICE"));
117
118             // Fill selection list with all classes.
119
TKVector classes = WebManThread.getClassVector();
120             int index = 0;
121             int size = classes.size();
122
123             while (index < size)
124             {
125
126                 String JavaDoc id = ((String JavaDoc[]) classes.elementAt(index++))[0];
127                 // Ignore hidden fields.
128
if (TKHiddenField.CLASS_ID.equals(id))
129                 {
130                     continue;
131                 }
132
133                 /* Commented out due to bug #49 !!!
134                 // Ignore frament fields within fragments.
135                 if (type == FRAGMENT_FORM_TYPE &&
136                     TKFragmentField.CLASS_ID.equals(id))
137                 {
138                     continue;
139                 }
140                 */

141
142                 TKBaseField field = (TKBaseField) TKFieldRegistry.registry.get(id);
143                 TKFieldGroup group = field.getDefGroup(allSwitch, allSwitchList);
144                 allSwitch.addAlternative(group);
145                 allSwitchList.addAlternative(group);
146             }
147
148             // Prepare field group definition.
149
TKBaseField [] definition = null;
150             String JavaDoc description = null;
151             switch (type)
152             {
153                 case CONTENT_FORM_TYPE:
154                 {
155                     description = LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_STRUCTURE");
156                     definition = new TKBaseField[]
157                     {
158                         // commented out due to bug #391
159
new TKHiddenField(TKFieldGroup.NAME_KEY),
160                         new TKInputField(TKFieldGroup.SHOW_NAME_KEY,
161                                          TKInputField.LARGE_DEFAULT_SIZE,
162                                          TKInputField.LARGE_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "CONTENT_STRUCTURE_SHOWNAME"), TKInputField.CHECK_STRING),
163                         allSwitchList
164                     };
165
166                     break;
167                 }
168                 case STRUCTURE_FORM_TYPE:
169                 {
170                     description = LanguageManager.getText(LANGUAGE_CONTEXT, "SITE_STRUCTURE_PARAMETER");
171                     definition = new TKBaseField[]
172                     {
173                         // commented out due to bug #391
174
new TKHiddenField(TKFieldGroup.NAME_KEY),
175                         new TKInputField(TKFieldGroup.SHOW_NAME_KEY,
176                                          TKInputField.LARGE_DEFAULT_SIZE,
177                                          TKInputField.LARGE_DEFAULT_LENGTH,
178                                          LanguageManager.getText(LANGUAGE_CONTEXT, "SITE_STRUCTURE_PARAMETER_SHOWNAME"), TKInputField.CHECK_STRING),
179                         allSwitchList
180                     };
181
182                     break;
183                 }
184                 case FRAGMENT_FORM_TYPE:
185                 {
186                     description = LanguageManager.getText(LANGUAGE_CONTEXT, "FRAGMENT");
187                     definition = new TKBaseField[]
188                     {
189                         allSwitch
190                     };
191
192                     break;
193                 }
194             default:
195                 // cant happen
196
}
197
198             return new TKFieldGroup(TKFieldGroup.CLASS_ID, new TKVector(definition), description);
199         }
200         catch (Exception JavaDoc x)
201         {
202             throw WebmanExceptionHandler.getException(x);
203         }
204     }
205
206     /**
207      * Generates a field name from the field description.
208      * <P>
209      * The field name will be the URL-encoded form of the
210      * description and not exceed th specified length.
211      *
212      * @param description the field description.
213      * @param length the maximum length of the field name.
214      * @return a field name.
215      */

216     public static String JavaDoc generateFieldName (String JavaDoc description, int length)
217     {
218         // Convert description.
219
String JavaDoc name = URLEncoder.encode(description);
220
221         // Return string of suitable length.
222
return name.length() > length
223                              ? name.substring(0, length)
224                              : name;
225     }
226
227     /**
228      * Checks wether the specified form definition exists.
229      *
230      * @param id the excluded id of the form definition (may be
231      * <CODE>null</CODE>).
232      * @param name the name of the search parameter.
233      * @param value the value of the search parameter.
234      * @return <CODE>true</CODE> if the specified form definition exists,
235      * otherwise <CODE>false</CODE>.
236      * @throws TKException if an error occurred during the check.
237      */

238     public static boolean exists (Integer JavaDoc id,
239                                   String JavaDoc name,
240                                   String JavaDoc value)
241         throws TKException
242     {
243         try
244         {
245             // Build appropriate query.
246
TKQuery query = TKWebmanDBManager.newQuery(FormExists.class);
247
248             if (id != null)
249             {
250                 query.setQueryParams(FormConstants.COLUMN_NAMES[FormConstants.FORM_ID], id);
251             }
252
253             query.setQueryParams(name, value);
254             query.execute();
255
256             // Fetch result.
257
ResultSet result = query.fetchResultSet();
258
259             if (result == null)
260             {
261                 return false;
262             }
263
264             // Check for exisisting form definitions.
265
return result.next();
266         }
267         catch (Exception JavaDoc e)
268         {
269             throw WebmanExceptionHandler.getException(e);
270         }
271     }
272
273     /**
274      * Checks wether sloppy naming is enabled.
275      *
276      * @return <CODE>true</CODE> if sloppy naming is enabled,
277      * otherwise <CODE>false</CODE>.
278      */

279     public static boolean isSloppyNamingEnabled ()
280     {
281         try
282         {
283             return Boolean.valueOf(PropertyManager.getPropertyManager(PROPERTY_GROUP_FORM_PROCESSING)
284                                                   .getValue(PROPERTY_ENABLE_SLOPPY_NAMING, Boolean.FALSE.toString()))
285                           .booleanValue();
286         }
287         catch (Exception JavaDoc e)
288         {
289             return false;
290         }
291     }
292
293     /**
294      * Checks wether unsafe modification is enabled.
295      *
296      * @return <CODE>true</CODE> if unsafe modification is enabled,
297      * otherwise <CODE>false</CODE>.
298      */

299     public static boolean isUnsafeModificationEnabled ()
300     {
301         try
302         {
303             return Boolean.valueOf(PropertyManager.getPropertyManager(PROPERTY_GROUP_FORM_PROCESSING)
304                                                   .getValue(PROPERTY_ENABLE_UNSAFE_MODIFICATION, Boolean.FALSE.toString()))
305                           .booleanValue();
306         }
307         catch (Exception JavaDoc e)
308         {
309             return false;
310         }
311     }
312
313     /**
314      * Checks wether the specified form name is ambiguous.
315      *
316      * @param id the optional form id to be excluded from the check.
317      * @param name the form name to be checked.
318      * @return <CODE>true</CODE> if the specified form name is ambiguous,
319      * otherwise <CODE>false</CODE>.
320      * @throws TKException if an error occurred during the check.
321      */

322     public static boolean isAmbiguousFormName (Integer JavaDoc id,
323                                                String JavaDoc name)
324         throws TKException
325     {
326         if (CTUtils.isSloppyNamingEnabled())
327         {
328             // No check.
329
return false;
330         }
331
332         // Check for existing names.
333
return CTUtils.exists(id, FormConstants.COLUMN_NAMES[FormConstants.FORM_NAME], name);
334     }
335
336     /**
337      * Checks wether the specified form description is ambiguous.
338      *
339      * @param id the optional form id to be excluded from the check.
340      * @param description the description name to be checked.
341      * @return <CODE>true</CODE> if the specified form description is ambiguous,
342      * otherwise <CODE>false</CODE>.
343      * @throws TKException if an error occurred during the check.
344      */

345     public static boolean isAmbiguousFormDescription (Integer JavaDoc id,
346                                                       String JavaDoc description)
347         throws TKException
348     {
349         if (CTUtils.isSloppyNamingEnabled())
350         {
351             // No check.
352
return false;
353         }
354
355         // Check for existing descriptions.
356
return CTUtils.exists(id, FormConstants.COLUMN_NAMES[FormConstants.FORM_DESCRIPTION], description);
357     }
358
359     /**
360      * Checks wether the specified event may cause an unsafe modification.
361      *
362      * @param event the event to be checked.
363      * @return <CODE>true</CODE> if the specified event may cause an unsafe modification,
364      * otherwise <CODE>false</CODE>.
365      */

366     public static boolean isUnsafeFormModification (TKEvent event)
367     {
368         if (CTUtils.isUnsafeModificationEnabled())
369         {
370             // No check.
371
return false;
372         }
373
374         // Get event parameters.
375
String JavaDoc id = event.getParameter(PARAMETER, "FORM_ID");
376         String JavaDoc force = event.getParameter(PARAMETER, "FORCE_MODIFICATION");
377
378         if (id == null || id.trim().length() == 0)
379         {
380             // Safe modification.
381
return false;
382         }
383
384         // Check for force flag.
385
return (force == null || force.trim().length() == 0);
386     }
387
388     /**
389      * Returns a parameter list from the specified event parameters.
390      *
391      * @param parameters the event parameters.
392      * @return a parameter list from the specified event parameters.
393      */

394     public static TKVector getParameterList (TKHashtable parameters)
395     {
396         TKVector list = new TKVector(parameters.size());
397
398         Enumeration keys = parameters.keys();
399
400         while (keys.hasMoreElements())
401         {
402             Object JavaDoc key = keys.nextElement();
403
404             if (key.toString().equals("CHECKER"))
405             {
406                 continue;
407             }
408
409             TKHashtable entry = new TKHashtable(2);
410             entry.put("PARAMETER_NAME", key);
411             entry.put("PARAMETER_VALUE", parameters.get(key));
412
413             list.addElement(entry);
414         }
415
416         return list;
417     }
418
419     /**
420      * Returns the form type description of the specified form.
421      *
422      * @param form the form.
423      * @return the form type description of the specified form.
424      * @throws TKException if any error occurred during form type
425      * description lookup.
426      */

427     public static String JavaDoc getFormTypeDescription (Form form)
428         throws TKException
429     {
430         if (form != null)
431         {
432             if (form.isFormFragment())
433             {
434                 return "FRAGMENT";
435             }
436
437             if (form.isContentForm())
438             {
439                 return "CONTENT_STRUCTURE";
440             }
441
442             if (form.isStructureForm())
443             {
444                 return "SITE_STRUCTURE_PARAMETER";
445             }
446         }
447
448         return "TEMPORARY_FORM";
449     }
450
451 }
452
Popular Tags