KickJava   Java API By Example, From Geeks To Geeks.

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


1 package de.webman.form.eventhandler;
2
3 import com.teamkonzept.web.*;
4 import com.teamkonzept.webman.*;
5 import com.teamkonzept.webman.db.TKWebmanDBManager;
6 import com.teamkonzept.webman.mainint.*;
7 import com.teamkonzept.webman.mainint.db.*;
8 import com.teamkonzept.webman.mainint.db.queries.*;
9 import com.teamkonzept.webman.mainint.events.*;
10 import com.teamkonzept.lib.*;
11 import com.teamkonzept.field.*;
12 import com.teamkonzept.field.db.*;
13 import com.teamkonzept.db.*;
14 import de.webman.form.Form;
15
16 /**
17  * Stores an existing form with a new name.
18  *
19  * @author $Author: uli $
20  * @version $Revision: 1.11 $
21  */

22 public class CTSaveAsHandler
23     extends DefaultEventHandler
24     implements ParameterTypes,
25                FrameConstants,
26                DatabaseDefaults
27 {
28
29     /**
30      * The singleton instance.
31      */

32     private static final CTSaveAsHandler INSTANCE = new CTSaveAsHandler();
33
34     /**
35      * Avoids outside instantiation.
36      */

37     private CTSaveAsHandler ()
38     {
39         // NOP
40
}
41
42     /**
43      * Returns the singleton instance.
44      *
45      * @return the singleton instance.
46      */

47     public static CTSaveAsHandler getInstance ()
48     {
49         return INSTANCE;
50     }
51
52     /**
53      * Handles the specified event.
54      *
55      * @param event the event to be handled.
56      * @throws TKException if any error occurred during event handling.
57      */

58     public void handleEvent (TKEvent event)
59         throws TKException
60     {
61         try
62         {
63             // Check event.
64
WebManEvent.checkEvent(event.getRemoteUser(), event.getName(), ContextConstants.CONTENT_TREE);
65
66             // Get form.
67
Form form = new Form(Integer.valueOf(event.getParameter(PARAMETER, "FORM_ID")));
68
69             // Get event parameters.
70
String JavaDoc formName = event.getParameter(PARAMETER, "NEW_FORM_NAME");
71             String JavaDoc formDescription = event.getParameter(PARAMETER, "NEW_FORM_DESCRIPTION");
72
73             if (formName == null || formName.trim().length() == 0)
74             {
75                 // Generate form name from description.
76
formName = CTUtils.generateFieldName(formDescription, TKInputField.SMALL_DEFAULT_LENGTH);
77             }
78             else
79             {
80                 if (CTUtils.isAmbiguousFormName(null, formName.toUpperCase()))
81                 {
82                     // Refuse ambiguous form name and terminate.
83
refuse(event, "FORM_NAME");
84                     return;
85                 }
86             }
87
88             if (CTUtils.isAmbiguousFormDescription(null, formDescription))
89             {
90                 // Refuse ambiguous form description and terminate.
91
refuse(event, "FORM_DESCRIPTION");
92                 return;
93             }
94
95             // Load existing form definition.
96
TKFormDBData data = new TKFormDBData(form.getIdentifierAsInt(), form.getTypeAsInt());
97             TKFormDBInterface.Get(data);
98             TKBaseField field = TKFieldRegistry.getFieldFromDB(data);
99
100             // Create new form definition.
101
TKFormDBData newDB = new TKFormDBData(0, form.getTypeAsInt());
102             TKFormDBInterface.New(newDB);
103             field.insertIntoDB(newDB);
104
105             // Store new form name / description.
106
((TKFieldTableData)newDB.field.get(0)).field_name = formName.toUpperCase();
107             ((TKFieldTableData)newDB.field.get(0)).field_show_name = formDescription;
108             TKFormDBInterface.Put(newDB);
109
110             // Forward event.
111
CTFramesHandler.getInstance().handleEvent(event);
112         }
113         catch (Throwable JavaDoc e)
114         {
115             // TO DO : Analyze Exception !
116
throw WebmanExceptionHandler.getException(e);
117         }
118     }
119
120     /**
121      * Checks wether this handler is capable to handle the specified event.
122      *
123      * @param event the event to be handled.
124      * @return <CODE>true</CODE> if this handler is capable to handle the
125      * specified event, otherwise <CODE>false</CODE>.
126      */

127     public boolean isHandler (TKEvent event)
128     {
129         return event.getName().equalsIgnoreCase("CT_SAVE_AS");
130     }
131
132     /**
133      * Refuses the specified event.
134      *
135      * @param event the event to be refused.
136      * @param error the cause of the refusal.
137      * @throws Throwable if any error occurred during event refusal.
138      */

139     private void refuse (TKEvent event, String JavaDoc error)
140         throws Throwable JavaDoc
141     {
142         // Set event parameters.
143
TKHTMLTemplate frame = event.getPrepHTMLTemplate("f_ct.tmpl");
144         frame.set("FORM_ID", event.getParameter(PARAMETER, "FORM_ID"));
145
146         if (event.getParameter(PARAMETER, "NEW_FORM_NAME") != null)
147         {
148             frame.set("NEW_FORM_NAME", event.getParameter(PARAMETER, "NEW_FORM_NAME"));
149         }
150
151         frame.set("NEW_FORM_DESCRIPTION", event.getParameter(PARAMETER, "NEW_FORM_DESCRIPTION"));
152         frame.set("ERROR", error);
153
154         // Forward event.
155
HTMLUtils.fillFrameSet(frame, LEFT_FRAME_WIDTH_SMALL, "CT_SHOWLIST", "CT_DUPLICATE");
156         WebManEvent.fillEventsIntoTemplate(event.getRemoteUser(), frame, CONTENT_TREE);
157         event.finishTemplate(frame);
158     }
159
160 }
161
Popular Tags