001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.books.frontend;
017:
018: import org.apache.cocoon.forms.event.ActionListener;
019: import org.apache.cocoon.forms.event.ActionEvent;
020: import org.apache.cocoon.forms.formmodel.Form;
021: import org.apache.cocoon.forms.formmodel.Repeater;
022: import org.apache.cocoon.forms.formmodel.Field;
023: import org.apache.cocoon.forms.formmodel.Action;
024: import org.apache.cocoon.forms.datatype.SelectionList;
025: import org.apache.cocoon.forms.datatype.Datatype;
026: import org.apache.cocoon.forms.datatype.StaticSelectionList;
027: import org.apache.cocoon.forms.util.StringMessage;
028: import org.apache.avalon.framework.service.ServiceManager;
029: import org.outerj.daisy.books.publisher.BookPublisher;
030: import org.outerj.daisy.books.publisher.PublicationSpec;
031: import org.outerj.daisy.books.publisher.PublicationTypeInfo;
032: import org.outerx.daisy.x10Bookpubspecs.PublicationSpecsDocument;
033:
034: import java.util.Map;
035: import java.util.Arrays;
036:
037: public class PublicationTypesFormHelper {
038: private static class AddPublicationTypeAction implements
039: ActionListener {
040: private final PublicationTypeInfo[] publicationTypeInfos;
041: private final ServiceManager serviceManager;
042:
043: public AddPublicationTypeAction(
044: PublicationTypeInfo[] publicationTypeInfos,
045: ServiceManager serviceManager) {
046: this .publicationTypeInfos = publicationTypeInfos;
047: this .serviceManager = serviceManager;
048: }
049:
050: public void actionPerformed(ActionEvent actionEvent) {
051: Form form = actionEvent.getSourceWidget().getForm();
052: String typeName = (String) form.lookupWidget(
053: "editors/gui/availablePublicationTypes").getValue();
054: Repeater publicationsRepeater = (Repeater) form
055: .lookupWidget("editors/gui/publications");
056: Repeater.RepeaterRow row = publicationsRepeater.addRow();
057: String typeLabel = getPublicationTypeLabel(typeName,
058: publicationTypeInfos);
059: row.getChild("typeLabel").setValue(typeLabel);
060: row.getChild("typeName").setValue(typeName);
061: row.getChild("outputName").setValue(typeName);
062: row.getChild("outputLabel").setValue(typeLabel);
063:
064: loadDefaultProperties(row, typeName, serviceManager);
065: }
066: }
067:
068: private static String getPublicationTypeLabel(String name,
069: PublicationTypeInfo[] publicationTypeInfos) {
070: for (PublicationTypeInfo publicationTypeInfo : publicationTypeInfos) {
071: if (publicationTypeInfo.getName().equals(name)) {
072: return publicationTypeInfo.getLabel();
073: }
074: }
075: return "(error: specified publication type unknown)";
076: }
077:
078: private static void loadDefaultProperties(Repeater.RepeaterRow row,
079: String publicationTypeName, ServiceManager serviceManager) {
080: Map<String, String> properties;
081: BookPublisher bookPublisher = null;
082: try {
083: bookPublisher = (BookPublisher) serviceManager
084: .lookup(BookPublisher.ROLE);
085: properties = bookPublisher
086: .getDefaultProperties(publicationTypeName);
087: } catch (Exception e) {
088: throw new RuntimeException(e);
089: } finally {
090: if (bookPublisher != null)
091: serviceManager.release(bookPublisher);
092: }
093: Field defaultPropertiesField = (Field) row
094: .getChild("defaultProperties");
095: defaultPropertiesField
096: .setSelectionList(buildDefaultPropertiesSelectionList(
097: properties, defaultPropertiesField
098: .getDatatype()));
099: defaultPropertiesField.setAttribute("properties", properties);
100: }
101:
102: private static SelectionList buildDefaultPropertiesSelectionList(
103: Map<String, String> properties, Datatype datatype) {
104: StaticSelectionList selectionList = new StaticSelectionList(
105: datatype);
106: for (Map.Entry<String, String> entry : properties.entrySet()) {
107: selectionList.addItem(entry.getKey(), new StringMessage(
108: entry.getKey() + " = " + entry.getValue()));
109: }
110: return selectionList;
111: }
112:
113: private static PublicationTypeInfo[] getPublicationTypeInfos(
114: ServiceManager serviceManager) throws Exception {
115: BookPublisher bookPublisher = null;
116: try {
117: bookPublisher = (BookPublisher) serviceManager
118: .lookup(BookPublisher.ROLE);
119: return bookPublisher.getAvailablePublicationTypes();
120: } finally {
121: if (bookPublisher != null)
122: serviceManager.release(bookPublisher);
123: }
124: }
125:
126: public static void initPublicationsForm(Form form,
127: ServiceManager serviceManager) throws Exception {
128: PublicationTypeInfo[] publicationTypeInfos = getPublicationTypeInfos(serviceManager);
129: Arrays.sort(publicationTypeInfos);
130: Field availablePublicationTypesField = (Field) form
131: .lookupWidget("editors/gui/availablePublicationTypes");
132: availablePublicationTypesField.setSelectionList(
133: publicationTypeInfos, "name", "label");
134: ((Action) form.lookupWidget("editors/gui/addPublicationType"))
135: .addActionListener(new AddPublicationTypeAction(
136: publicationTypeInfos, serviceManager));
137: }
138:
139: public static void loadPublicationSpecs(Form form,
140: PublicationSpec[] specs, ServiceManager serviceManager)
141: throws Exception {
142: PublicationTypeInfo[] publicationTypeInfos = getPublicationTypeInfos(serviceManager);
143: Repeater repeater = (Repeater) form
144: .lookupWidget("editors/gui/publications");
145: repeater.clear(); // clear in case this is called on an existing form
146: for (PublicationSpec spec : specs) {
147: Repeater.RepeaterRow row = repeater.addRow();
148: row.getChild("typeLabel").setValue(
149: getPublicationTypeLabel(spec
150: .getPublicationTypeName(),
151: publicationTypeInfos));
152: row.getChild("typeName").setValue(
153: spec.getPublicationTypeName());
154: row.getChild("outputName").setValue(
155: spec.getPublicationOutputName());
156: row.getChild("outputLabel").setValue(
157: spec.getPublicationOutputLabel());
158: Repeater propertiesRepeater = (Repeater) row
159: .getChild("properties");
160: for (Map.Entry<String, String> entry : spec
161: .getPublicationProperties().entrySet()) {
162: Repeater.RepeaterRow propRow = propertiesRepeater
163: .addRow();
164: propRow.getChild("name").setValue(entry.getKey());
165: propRow.getChild("value").setValue(entry.getValue());
166: }
167: loadDefaultProperties(row, spec.getPublicationTypeName(),
168: serviceManager);
169: }
170: }
171:
172: public static PublicationSpecsDocument getXml(Form form) {
173: PublicationSpecsDocument publicationSpecsDocument = PublicationSpecsDocument.Factory
174: .newInstance();
175: PublicationSpecsDocument.PublicationSpecs publicationSpecsXml = publicationSpecsDocument
176: .addNewPublicationSpecs();
177:
178: Repeater repeater = (Repeater) form
179: .lookupWidget("editors/gui/publications");
180: for (int i = 0; i < repeater.getSize(); i++) {
181: Repeater.RepeaterRow row = repeater.getRow(i);
182: String typeName = (String) row.getChild("typeName")
183: .getValue();
184: String outputName = (String) row.getChild("outputName")
185: .getValue();
186: String outputLabel = (String) row.getChild("outputLabel")
187: .getValue();
188:
189: PublicationSpecsDocument.PublicationSpecs.PublicationSpec publicationSpecXml = publicationSpecsXml
190: .addNewPublicationSpec();
191: publicationSpecXml.setName(outputName);
192: publicationSpecXml.setType(typeName);
193: publicationSpecXml.setLabel(outputLabel);
194:
195: Repeater propertiesRepeater = (Repeater) row
196: .getChild("properties");
197: if (propertiesRepeater.getSize() > 0) {
198: PublicationSpecsDocument.PublicationSpecs.PublicationSpec.Properties propertiesXml = publicationSpecXml
199: .addNewProperties();
200: for (int k = 0; k < propertiesRepeater.getSize(); k++) {
201: Repeater.RepeaterRow propRow = propertiesRepeater
202: .getRow(k);
203: String name = (String) propRow.getChild("name")
204: .getValue();
205: String value = (String) propRow.getChild("value")
206: .getValue();
207: if (name != null) {
208: PublicationSpecsDocument.PublicationSpecs.PublicationSpec.Properties.Entry entryXml = propertiesXml
209: .addNewEntry();
210: entryXml.setKey(name);
211: if (value != null)
212: entryXml.setStringValue(value);
213: }
214: }
215: }
216: }
217:
218: return publicationSpecsDocument;
219: }
220: }
|