KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > admin > FieldTypeEditorApple


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend.admin;
17
18 import org.outerj.daisy.frontend.util.AbstractDaisyApple;
19 import org.outerj.daisy.frontend.util.FormHelper;
20 import org.outerj.daisy.frontend.PageContext;
21 import org.outerj.daisy.frontend.SkinConfHelper;
22 import org.outerj.daisy.frontend.WikiHelper;
23 import org.outerj.daisy.frontend.editor.LinkFieldHelper;
24 import org.outerj.daisy.repository.schema.*;
25 import org.outerj.daisy.repository.Repository;
26 import org.outerj.daisy.repository.ValueType;
27 import org.outerj.daisy.repository.VariantKey;
28 import org.outerj.daisy.repository.query.SortOrder;
29 import org.outerj.daisy.repository.variant.VariantManager;
30 import org.apache.cocoon.components.flow.apples.AppleRequest;
31 import org.apache.cocoon.components.flow.apples.AppleResponse;
32 import org.apache.cocoon.forms.binding.Binding;
33 import org.apache.cocoon.forms.formmodel.*;
34 import org.apache.cocoon.forms.FormContext;
35 import org.apache.avalon.framework.service.ServiceManager;
36 import org.apache.avalon.framework.service.ServiceException;
37 import org.apache.avalon.framework.service.Serviceable;
38 import org.apache.avalon.framework.activity.Initializable;
39
40 import java.util.HashMap JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Locale JavaDoc;
43
44 public class FieldTypeEditorApple extends AbstractDaisyApple implements Serviceable, Initializable {
45     private ServiceManager serviceManager;
46     private AdminLocales.Locales locales;
47     private Form form;
48     private Binding binding;
49     private RepositorySchema repositorySchema;
50     private VariantManager variantManager;
51     private FieldType fieldType;
52     private boolean init = false;
53     private Locale JavaDoc locale;
54     private Map JavaDoc viewData;
55
56     public void service(ServiceManager serviceManager) throws ServiceException {
57         this.serviceManager = serviceManager;
58     }
59
60     public void initialize() throws Exception JavaDoc {
61         AdminLocales adminLocales = (AdminLocales)serviceManager.lookup(AdminLocales.ROLE);
62         try {
63             locales = adminLocales.getLocales();
64         } finally {
65             serviceManager.release(adminLocales);
66         }
67     }
68
69     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
70         if (!init) {
71             locale = WikiHelper.getLocale(appleRequest.getCocoonRequest());
72             form = FormHelper.createForm(serviceManager, "resources/form/fieldtype_definition.xml");
73             binding = FormHelper.createBinding(serviceManager, "resources/form/fieldtype_binding.xml");
74             Repository repository = WikiHelper.getRepository(appleRequest.getCocoonRequest(), serviceManager);
75             repositorySchema = repository.getRepositorySchema();
76             variantManager = repository.getVariantManager();
77
78             // initialiaze repeater for displaydata with the locales we allow to be edited
79
String JavaDoc[] supportedLocales = locales.getAsStrings();
80             Repeater repeater = (Repeater)form.getChild("displaydata");
81             for (int i = 0; i < supportedLocales.length; i++) {
82                 Repeater.RepeaterRow row = repeater.addRow();
83                 row.getChild("locale").setValue(supportedLocales[i]);
84             }
85
86             form.getChild("valuetype").setValue(ValueType.STRING.toString());
87             form.setAttribute("variantManager", variantManager); // so that it is accessible to validators
88

89             initStaticSelectionList();
90
91             String JavaDoc fieldTypeId = appleRequest.getSitemapParameter("id");
92             if (fieldTypeId != null) {
93                 fieldType = repositorySchema.getFieldTypeById(Long.parseLong(fieldTypeId), true);
94                 binding.loadFormFromModel(form, fieldType);
95                 form.getChild("valuetype").setValue(fieldType.getValueType().toString());
96                 loadSelectionList();
97                 form.getChild("valuetype").setState(WidgetState.OUTPUT);
98                 form.getChild("multivalue").setState(WidgetState.OUTPUT);
99             } else {
100                 fieldTypeId = "new";
101                 form.getChild("size").setValue(new Integer JavaDoc(0));
102             }
103
104             String JavaDoc path = getMountPoint() + "/admin/fieldType/" + fieldTypeId + "/edit/" + getContinuationId();
105
106             viewData = new HashMap JavaDoc();
107             viewData.put("submitPath", path);
108             viewData.put("mountPoint", getMountPoint());
109             if (fieldType != null)
110                 viewData.put("fieldType", fieldType);
111             viewData.put("locale", locale);
112             viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
113             viewData.put("CocoonFormsInstance", form);
114             viewData.put("entityName", "fieldtype");
115
116             init = true;
117
118             appleResponse.redirectTo(path);
119         } else {
120             String JavaDoc methodName = appleRequest.getCocoonRequest().getMethod();
121             if (methodName.equals("GET")) {
122                 // display the form
123
viewData.put("valuetype", getValueType().toString());
124                 appleResponse.sendPage("Form-fieldtype-Pipe", viewData);
125             } else if (methodName.equals("POST")) {
126                 // handle a form submit
127
boolean endProcessing = form.process(new FormContext(appleRequest.getCocoonRequest(), locale));
128
129                 if (!endProcessing) {
130                     viewData.put("valuetype", getValueType().toString());
131                     appleResponse.sendPage("Form-fieldtype-Pipe", viewData);
132                 } else {
133                     if (fieldType == null) {
134                         boolean multiValue = ((Boolean JavaDoc)form.getChild("multivalue").getValue()).booleanValue();
135                         fieldType = repositorySchema.createFieldType((String JavaDoc)form.getChild("name").getValue(),
136                             getValueType(), multiValue);
137                         viewData.put("fieldType", fieldType);
138                     }
139
140                     binding.saveFormToModel(form, fieldType);
141                     storeSelectionList();
142                     fieldType.save();
143                     appleResponse.redirectTo(getMountPoint() + "/admin/fieldType");
144                 }
145             } else {
146                 throw new Exception JavaDoc("Unspported HTTP method: " + methodName);
147             }
148         }
149     }
150
151     private void initStaticSelectionList() {
152         Repeater localesRepeater = (Repeater)form.lookupWidget("selectionlist/static/locales");
153         String JavaDoc[] locales = this.locales.getAsStrings();
154         for (int i = 0; i < locales.length; i++) {
155             localesRepeater.addRow().getChild("locale").setValue(locales[i]);
156         }
157
158         Repeater selectionListRepeater = (Repeater)form.lookupWidget("selectionlist/static/selectionlistdata");
159         Repeater.RepeaterRow row = selectionListRepeater.addRow();
160         addSelectionListLabelEntries(row);
161     }
162
163     private void addSelectionListLabelEntries(Repeater.RepeaterRow row) {
164         Repeater labelsRepeater = (Repeater)row.getChild("localizedLabels");
165         int localeCount = locales.getAsStrings().length;
166         for (int i = 0; i < localeCount; i++) {
167             labelsRepeater.addRow();
168         }
169     }
170     private void loadSelectionList() {
171         SelectionList selectionList = fieldType.getSelectionList();
172         if (selectionList == null) {
173             // do nothing
174
} else if (selectionList instanceof StaticSelectionList) {
175             form.getChild("selectionlist_type").setValue("static");
176             boolean isLinkType = fieldType.getValueType() == ValueType.LINK;
177             Repeater repeater = (Repeater)form.lookupWidget("selectionlist/static/selectionlistdata");
178             repeater.clear();
179             String JavaDoc valueWidgetId = "value/" + fieldType.getValueType();
180             Locale JavaDoc[] locales = this.locales.getAsObjects();
181             ListItem[] items = selectionList.getItems();
182             for (int i = 0; i < items.length; i++) {
183                 Repeater.RepeaterRow row = repeater.addRow();
184                 Object JavaDoc value = items[i].getValue();
185                 if (isLinkType) {
186                     value = LinkFieldHelper.variantKeyToString((VariantKey)items[i].getValue(), variantManager);
187                 }
188                 row.lookupWidget(valueWidgetId).setValue(value);
189                 for (int k = 0; k < locales.length; k++) {
190                     Repeater labelsRepeater = (Repeater)row.getChild("localizedLabels");
191                     labelsRepeater.addRow().getChild("label").setValue(items[i].getLabelExact(locales[k]));
192                 }
193             }
194         } else if (selectionList instanceof LinkQuerySelectionList) {
195             form.getChild("selectionlist_type").setValue("linkquery");
196             LinkQuerySelectionList lqSellist = (LinkQuerySelectionList)selectionList;
197             form.lookupWidget("selectionlist/linkquery/whereClause").setValue(lqSellist.getWhereClause());
198             form.lookupWidget("selectionlist/linkquery/filterVariants").setValue((lqSellist.getFilterVariants()) ? Boolean.TRUE : Boolean.FALSE);
199         } else if (selectionList instanceof QuerySelectionList) {
200             form.getChild("selectionlist_type").setValue("query");
201             QuerySelectionList querySelectionList = (QuerySelectionList)selectionList;
202             form.lookupWidget("selectionlist/query/query").setValue(querySelectionList.getQuery());
203             form.lookupWidget("selectionlist/query/filterVariants").setValue((querySelectionList.getFilterVariants()) ? Boolean.TRUE : Boolean.FALSE);
204             form.lookupWidget("selectionlist/query/sortOrder").setValue(querySelectionList.getSortOrder());
205         }
206     }
207
208     private void storeSelectionList() {
209         String JavaDoc selectionListType = (String JavaDoc)form.getChild("selectionlist_type").getValue();
210         if (selectionListType.equals("static")) {
211             storeStaticSelectionList();
212         } else if (selectionListType.equals("linkquery")) {
213             storeLinkQuerySelectionList();
214         } else if (selectionListType.equals("query")) {
215             storeQuerySelectionList();
216         } else {
217             fieldType.clearSelectionList();
218         }
219     }
220
221     private void storeStaticSelectionList() {
222         Repeater repeater = (Repeater)form.lookupWidget("selectionlist/static/selectionlistdata");
223         boolean isLinkType = fieldType.getValueType() == ValueType.LINK;
224         if (repeater.getSize() == 0) {
225             fieldType.clearSelectionList();
226         } else {
227             String JavaDoc valueWidgetId = "value/" + fieldType.getValueType();
228             Locale JavaDoc[] locales = this.locales.getAsObjects();
229             fieldType.clearSelectionList();
230             StaticSelectionList selectionList = fieldType.createStaticSelectionList();
231             for (int i = 0; i < repeater.getSize(); i++) {
232                 Repeater.RepeaterRow row = repeater.getRow(i);
233                 Object JavaDoc value = row.lookupWidget(valueWidgetId).getValue();
234                 if (isLinkType) {
235                     value = LinkFieldHelper.parseVariantKey((String JavaDoc)value, variantManager);
236                 }
237                 StaticListItem staticListItem = selectionList.createStaticListItem(value);
238                 Repeater labelRepeater = (Repeater)row.getChild("localizedLabels");
239                 for (int k = 0; k < locales.length; k++) {
240                     String JavaDoc label = (String JavaDoc)labelRepeater.getRow(k).getChild("label").getValue();
241                     if (label != null) {
242                         staticListItem.setLabel(locales[k], label);
243                     }
244                 }
245             }
246         }
247     }
248
249     private void storeLinkQuerySelectionList() {
250         if (getValueType() != ValueType.LINK)
251             return;
252
253         String JavaDoc whereClause = (String JavaDoc)form.lookupWidget("selectionlist/linkquery/whereClause").getValue();
254         boolean filterVariants = ((Boolean JavaDoc)form.lookupWidget("selectionlist/linkquery/filterVariants").getValue()).booleanValue();
255         fieldType.createLinkQuerySelectionList(whereClause, filterVariants);
256     }
257
258     private void storeQuerySelectionList() {
259         String JavaDoc query = (String JavaDoc)form.lookupWidget("selectionlist/query/query").getValue();
260         boolean filterVariants = ((Boolean JavaDoc)form.lookupWidget("selectionlist/query/filterVariants").getValue()).booleanValue();
261         SortOrder sortOrder = (SortOrder)form.lookupWidget("selectionlist/query/sortOrder").getValue();
262         fieldType.createQuerySelectionList(query, filterVariants, sortOrder);
263     }
264
265     private ValueType getValueType() {
266         return fieldType != null ? fieldType.getValueType() : ValueType.fromString((String JavaDoc)form.getChild("valuetype").getValue());
267     }
268 }
269
Popular Tags