001: package de.webman.form.eventhandler;
002:
003: import java.util.*;
004: import java.sql.*;
005: import java.io.*;
006: import java.net.*;
007: import com.teamkonzept.web.*;
008: import com.teamkonzept.lib.*;
009: import com.teamkonzept.field.*;
010: import com.teamkonzept.field.db.TKFormDBData;
011: import com.teamkonzept.field.db.TKFormDBInterface;
012: import com.teamkonzept.db.*;
013: import com.teamkonzept.webman.mainint.*;
014: import com.teamkonzept.publishing.markups.*;
015: import com.teamkonzept.international.LanguageManager;
016: import com.teamkonzept.webman.*;
017: import com.teamkonzept.webman.db.*;
018: import com.teamkonzept.webman.mainint.*;
019: import com.teamkonzept.webman.mainint.events.*;
020: import com.teamkonzept.webman.mainint.db.*;
021: import com.teamkonzept.webman.mainint.db.queries.*;
022: import de.webman.form.Form;
023: import de.webman.form.db.FormConstants;
024: import de.webman.form.db.queries.FormExists;
025:
026: /**
027: * Provides convenience methods used by content type event handlers.
028: *
029: * @author $Author: uli $
030: * @version $Revision: 1.11 $
031: */
032: public class CTUtils implements CTParameters, ParameterTypes,
033: DatabaseDefaults {
034:
035: /**
036: * The language manager context.
037: */
038: public static final String LANGUAGE_CONTEXT = "field";
039:
040: /**
041: * The <CODE>form processing</CODE> property group.
042: */
043: public static final String PROPERTY_GROUP_FORM_PROCESSING = "FORM_PROCESSING";
044:
045: /**
046: * The <CODE>enable sloppy naming</CODE> property.
047: */
048: public static final String PROPERTY_ENABLE_SLOPPY_NAMING = "ENABLE_SLOPPY_NAMING";
049:
050: /**
051: * The <CODE>enable unsafe modification</CODE> property.
052: */
053: public static final String PROPERTY_ENABLE_UNSAFE_MODIFICATION = "ENABLE_UNSAFE_MODIFICATION";
054:
055: /**
056: * Keeps the context by passing all parameters of the event
057: * into the given template.
058: *
059: * @param event the event.
060: * @param template the template.
061: */
062: public static void keepCTContext(TKEvent event,
063: TKHTMLTemplate template) {
064: template.set(FORM_TYPE, event.getNotNullParameter(PARAMETER,
065: FORM_TYPE));
066: template.set(FORM_ID, event.getNotNullParameter(PARAMETER,
067: FORM_ID));
068: template.set(ISTEST, event.getNotNullParameter(PARAMETER,
069: ISTEST));
070: template.set(MODIFY_CONTEXT, event.getNotNullParameter(
071: PARAMETER, MODIFY_CONTEXT));
072: template.set(TMP_FORM_ID, event.getNotNullParameter(PARAMETER,
073: TMP_FORM_ID));
074: template.set(UPBASE, event.getParams().hasMultiple(PARAMETER,
075: UPBASE) ? event.getParams().get(PARAMETER, UPBASE, 0)
076: : event.getNotNullParameter(PARAMETER, UPBASE));
077: }
078:
079: /**
080: * Switches between normal and testing mode by setting the
081: * according parameters in the template.
082: *
083: * @param template the template.
084: * @param test specifies the mode.
085: */
086: public static void switchTest(TKHTMLTemplate template, boolean test) {
087: if (test) {
088: template.set(ISTEST, "testing");
089: template.set(MODIFY_CONTEXT, CTTEST);
090: } else {
091: template.set(TMP_FORM_ID, EMPTY);
092: template.set(ISTEST, EMPTY);
093: template.set(MODIFY_CONTEXT, CT);
094: }
095: }
096:
097: /**
098: * Creates the top-level field structure based on the given form type.
099: *
100: * @param type the form type.
101: * @return the top-level field structure based on the given form type.
102: * @throws TKException if an error occurred during structure creation.
103: */
104: public static TKBaseField getStructure(int type) throws TKException {
105: try {
106: // Create simple selection with option check.
107: TKFieldSwitch allSwitch = new TKFieldSwitch(
108: TKBaseField.SUB_TYPE_KEY,
109: new TKVector(),
110: LanguageManager.getText(LANGUAGE_CONTEXT, "CHOICE"),
111: TKFieldSwitch.CHECK_OPTION);
112:
113: // Create selection list.
114: TKFieldSwitchList allSwitchList = new TKFieldSwitchList(
115: TKBaseField.SUB_LIST_KEY, new TKVector(),
116: 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: String id = ((String[]) classes.elementAt(index++))[0];
126: // Ignore hidden fields.
127: if (TKHiddenField.CLASS_ID.equals(id)) {
128: continue;
129: }
130:
131: /* Commented out due to bug #49 !!!
132: // Ignore frament fields within fragments.
133: if (type == FRAGMENT_FORM_TYPE &&
134: TKFragmentField.CLASS_ID.equals(id))
135: {
136: continue;
137: }
138: */
139:
140: TKBaseField field = (TKBaseField) TKFieldRegistry.registry
141: .get(id);
142: TKFieldGroup group = field.getDefGroup(allSwitch,
143: allSwitchList);
144: allSwitch.addAlternative(group);
145: allSwitchList.addAlternative(group);
146: }
147:
148: // Prepare field group definition.
149: TKBaseField[] definition = null;
150: String description = null;
151: switch (type) {
152: case CONTENT_FORM_TYPE: {
153: description = LanguageManager.getText(LANGUAGE_CONTEXT,
154: "CONTENT_STRUCTURE");
155: definition = new TKBaseField[] {
156: // commented out due to bug #391
157: new TKHiddenField(TKFieldGroup.NAME_KEY),
158: new TKInputField(TKFieldGroup.SHOW_NAME_KEY,
159: TKInputField.LARGE_DEFAULT_SIZE,
160: TKInputField.LARGE_DEFAULT_LENGTH,
161: LanguageManager.getText(
162: LANGUAGE_CONTEXT,
163: "CONTENT_STRUCTURE_SHOWNAME"),
164: TKInputField.CHECK_STRING),
165: allSwitchList };
166:
167: break;
168: }
169: case STRUCTURE_FORM_TYPE: {
170: description = LanguageManager.getText(LANGUAGE_CONTEXT,
171: "SITE_STRUCTURE_PARAMETER");
172: definition = new TKBaseField[] {
173: // commented out due to bug #391
174: new TKHiddenField(TKFieldGroup.NAME_KEY),
175: new TKInputField(
176: TKFieldGroup.SHOW_NAME_KEY,
177: TKInputField.LARGE_DEFAULT_SIZE,
178: TKInputField.LARGE_DEFAULT_LENGTH,
179: LanguageManager
180: .getText(LANGUAGE_CONTEXT,
181: "SITE_STRUCTURE_PARAMETER_SHOWNAME"),
182: TKInputField.CHECK_STRING),
183: allSwitchList };
184:
185: break;
186: }
187: case FRAGMENT_FORM_TYPE: {
188: description = LanguageManager.getText(LANGUAGE_CONTEXT,
189: "FRAGMENT");
190: definition = new TKBaseField[] { allSwitch };
191:
192: break;
193: }
194: default:
195: // cant happen
196: }
197:
198: return new TKFieldGroup(TKFieldGroup.CLASS_ID,
199: new TKVector(definition), description);
200: } catch (Exception x) {
201: throw WebmanExceptionHandler.getException(x);
202: }
203: }
204:
205: /**
206: * Generates a field name from the field description.
207: * <P>
208: * The field name will be the URL-encoded form of the
209: * description and not exceed th specified length.
210: *
211: * @param description the field description.
212: * @param length the maximum length of the field name.
213: * @return a field name.
214: */
215: public static String generateFieldName(String description,
216: int length) {
217: // Convert description.
218: String name = URLEncoder.encode(description);
219:
220: // Return string of suitable length.
221: return name.length() > length ? name.substring(0, length)
222: : name;
223: }
224:
225: /**
226: * Checks wether the specified form definition exists.
227: *
228: * @param id the excluded id of the form definition (may be
229: * <CODE>null</CODE>).
230: * @param name the name of the search parameter.
231: * @param value the value of the search parameter.
232: * @return <CODE>true</CODE> if the specified form definition exists,
233: * otherwise <CODE>false</CODE>.
234: * @throws TKException if an error occurred during the check.
235: */
236: public static boolean exists(Integer id, String name, String value)
237: throws TKException {
238: try {
239: // Build appropriate query.
240: TKQuery query = TKWebmanDBManager
241: .newQuery(FormExists.class);
242:
243: if (id != null) {
244: query
245: .setQueryParams(
246: FormConstants.COLUMN_NAMES[FormConstants.FORM_ID],
247: id);
248: }
249:
250: query.setQueryParams(name, value);
251: query.execute();
252:
253: // Fetch result.
254: ResultSet result = query.fetchResultSet();
255:
256: if (result == null) {
257: return false;
258: }
259:
260: // Check for exisisting form definitions.
261: return result.next();
262: } catch (Exception e) {
263: throw WebmanExceptionHandler.getException(e);
264: }
265: }
266:
267: /**
268: * Checks wether sloppy naming is enabled.
269: *
270: * @return <CODE>true</CODE> if sloppy naming is enabled,
271: * otherwise <CODE>false</CODE>.
272: */
273: public static boolean isSloppyNamingEnabled() {
274: try {
275: return Boolean.valueOf(
276: PropertyManager.getPropertyManager(
277: PROPERTY_GROUP_FORM_PROCESSING).getValue(
278: PROPERTY_ENABLE_SLOPPY_NAMING,
279: Boolean.FALSE.toString())).booleanValue();
280: } catch (Exception e) {
281: return false;
282: }
283: }
284:
285: /**
286: * Checks wether unsafe modification is enabled.
287: *
288: * @return <CODE>true</CODE> if unsafe modification is enabled,
289: * otherwise <CODE>false</CODE>.
290: */
291: public static boolean isUnsafeModificationEnabled() {
292: try {
293: return Boolean.valueOf(
294: PropertyManager.getPropertyManager(
295: PROPERTY_GROUP_FORM_PROCESSING).getValue(
296: PROPERTY_ENABLE_UNSAFE_MODIFICATION,
297: Boolean.FALSE.toString())).booleanValue();
298: } catch (Exception e) {
299: return false;
300: }
301: }
302:
303: /**
304: * Checks wether the specified form name is ambiguous.
305: *
306: * @param id the optional form id to be excluded from the check.
307: * @param name the form name to be checked.
308: * @return <CODE>true</CODE> if the specified form name is ambiguous,
309: * otherwise <CODE>false</CODE>.
310: * @throws TKException if an error occurred during the check.
311: */
312: public static boolean isAmbiguousFormName(Integer id, String name)
313: throws TKException {
314: if (CTUtils.isSloppyNamingEnabled()) {
315: // No check.
316: return false;
317: }
318:
319: // Check for existing names.
320: return CTUtils.exists(id,
321: FormConstants.COLUMN_NAMES[FormConstants.FORM_NAME],
322: name);
323: }
324:
325: /**
326: * Checks wether the specified form description is ambiguous.
327: *
328: * @param id the optional form id to be excluded from the check.
329: * @param description the description name to be checked.
330: * @return <CODE>true</CODE> if the specified form description is ambiguous,
331: * otherwise <CODE>false</CODE>.
332: * @throws TKException if an error occurred during the check.
333: */
334: public static boolean isAmbiguousFormDescription(Integer id,
335: String description) throws TKException {
336: if (CTUtils.isSloppyNamingEnabled()) {
337: // No check.
338: return false;
339: }
340:
341: // Check for existing descriptions.
342: return CTUtils
343: .exists(
344: id,
345: FormConstants.COLUMN_NAMES[FormConstants.FORM_DESCRIPTION],
346: description);
347: }
348:
349: /**
350: * Checks wether the specified event may cause an unsafe modification.
351: *
352: * @param event the event to be checked.
353: * @return <CODE>true</CODE> if the specified event may cause an unsafe modification,
354: * otherwise <CODE>false</CODE>.
355: */
356: public static boolean isUnsafeFormModification(TKEvent event) {
357: if (CTUtils.isUnsafeModificationEnabled()) {
358: // No check.
359: return false;
360: }
361:
362: // Get event parameters.
363: String id = event.getParameter(PARAMETER, "FORM_ID");
364: String force = event.getParameter(PARAMETER,
365: "FORCE_MODIFICATION");
366:
367: if (id == null || id.trim().length() == 0) {
368: // Safe modification.
369: return false;
370: }
371:
372: // Check for force flag.
373: return (force == null || force.trim().length() == 0);
374: }
375:
376: /**
377: * Returns a parameter list from the specified event parameters.
378: *
379: * @param parameters the event parameters.
380: * @return a parameter list from the specified event parameters.
381: */
382: public static TKVector getParameterList(TKHashtable parameters) {
383: TKVector list = new TKVector(parameters.size());
384:
385: Enumeration keys = parameters.keys();
386:
387: while (keys.hasMoreElements()) {
388: Object key = keys.nextElement();
389:
390: if (key.toString().equals("CHECKER")) {
391: continue;
392: }
393:
394: TKHashtable entry = new TKHashtable(2);
395: entry.put("PARAMETER_NAME", key);
396: entry.put("PARAMETER_VALUE", parameters.get(key));
397:
398: list.addElement(entry);
399: }
400:
401: return list;
402: }
403:
404: /**
405: * Returns the form type description of the specified form.
406: *
407: * @param form the form.
408: * @return the form type description of the specified form.
409: * @throws TKException if any error occurred during form type
410: * description lookup.
411: */
412: public static String getFormTypeDescription(Form form)
413: throws TKException {
414: if (form != null) {
415: if (form.isFormFragment()) {
416: return "FRAGMENT";
417: }
418:
419: if (form.isContentForm()) {
420: return "CONTENT_STRUCTURE";
421: }
422:
423: if (form.isStructureForm()) {
424: return "SITE_STRUCTURE_PARAMETER";
425: }
426: }
427:
428: return "TEMPORARY_FORM";
429: }
430:
431: }
|