001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Admin/Sources/es/udc/mypersonalizer/admin/http/controller/actions/propertyeditor/AbstractUpdatePropertyAction.java,v 1.1.1.1 2004/03/25 12:08:38 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:38 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.admin.http.controller.actions.propertyeditor;
042:
043: import java.util.Collection;
044: import java.util.ArrayList;
045:
046: import javax.servlet.http.HttpServletRequest;
047: import javax.servlet.http.HttpServletResponse;
048:
049: import org.apache.struts.action.ActionForm;
050: import org.apache.struts.action.ActionMapping;
051: import org.apache.struts.action.ActionForward;
052:
053: import es.udc.mypersonalizer.kernel.model.properties.PropertyStructure;
054: import es.udc.mypersonalizer.kernel.model.metainfo.MetaProperty;
055: import es.udc.mypersonalizer.kernel.model.properties.Property;
056: import es.udc.mypersonalizer.kernel.model.properties.CompoundProperty;
057:
058: import es.udc.mypersonalizer.admin.http.controller.actions.propertyeditor.interfaces.PropertyEditorConfigurationRegistry;
059: import es.udc.mypersonalizer.admin.http.controller.actions.propertyeditor.interfaces.PropertyAccessStrategy;
060: import es.udc.mypersonalizer.admin.http.view.viewobjects.propertyeditor.ViewPropertyValue;
061: import es.udc.mypersonalizer.admin.http.view.viewobjects.propertyeditor.ViewPropertyInformation;
062: import es.udc.mypersonalizer.admin.http.view.actionforms.propertyeditor.UpdatePropertyForm;
063: import es.udc.mypersonalizer.admin.http.controller.actions.propertyeditor.util.PropertyEditorHelper;
064: import es.udc.mypersonalizer.admin.http.controller.actions.util.ControllerHelper;
065:
066: /**
067: * This abstract class performs common tasks when trying to update simple or
068: * compound properties. It implements the Template Method pattern to take care
069: * of the differences between updating a simple property and a compound one.
070: * <p>
071: * This class basically retrives parameters from {@link UpdatePropertyForm},
072: * then gets a description of the current property to update and finally
073: * forwards to <code>UpdateMultiValuedPropertyJSP</code> or
074: * <code>UpdateSingleValuedPropertyJSP</code> pages.
075: * <p>
076: * If the selected property for update is compound and single-valued with
077: * 0 values, this action creates and initializes a value for updating it,
078: * and then (after the forward) lets the user edit it in a JSP frm.
079: *
080: * @author Abel Iago Toral Quiroga
081: * @since 1.0
082: */
083: public abstract class AbstractUpdatePropertyAction extends
084: PropertyEditorAction {
085:
086: protected ActionForward doExecuteWork(ActionMapping mapping,
087: ActionForm form, HttpServletRequest request,
088: HttpServletResponse response) throws Exception {
089:
090: /* Get parameters */
091: UpdatePropertyForm frm = (UpdatePropertyForm) form;
092: String peConfigurationIdentifier = frm.getPeConfId();
093: String serviceIdentifier = frm.getServId();
094: String propertyIdentifier = frm.getPropId();
095: String propertyStructureName = frm.getPropertyStructureName();
096: String propertySimpleName = frm.getPropertySimpleName();
097:
098: /* Get the property access strategy */
099: PropertyAccessStrategy propertyAccessStrategy = PropertyEditorConfigurationRegistry
100: .getConfiguration(peConfigurationIdentifier)
101: .getPropertyAccessStrategy();
102:
103: /* Get the root property that is being edited */
104: Property rootProperty = propertyAccessStrategy.findProperty(
105: request, propertyIdentifier);
106: String rootPropertyName = rootProperty.getSimpleName();
107:
108: /* Find the property to update in the root property */
109: Property currentProperty = PropertyEditorHelper
110: .findProperty(rootProperty, propertyStructureName,
111: propertySimpleName);
112: int numberOfValues = currentProperty.getValuesAsObject().length;
113:
114: /* Get meta-info of the property */
115: MetaProperty currentMetaProperty = PropertyEditorHelper
116: .findMetaProperty(serviceIdentifier,
117: propertyStructureName, propertySimpleName);
118:
119: /* If property is compound and single-valued, and has no value,
120: we must create an empty value to edit it*/
121: if ((numberOfValues == 0)
122: && (currentProperty instanceof CompoundProperty)
123: && (!currentMetaProperty.isMultiValued())) {
124:
125: String propertyAbsoluteName = ((propertyStructureName
126: .equals("")) ? ""
127: : (propertyStructureName + "." + propertySimpleName));
128:
129: PropertyStructure value = ControllerHelper
130: .createEmptyPropertyStructureValue(
131: serviceIdentifier, propertyAbsoluteName);
132: PropertyStructure values[] = new PropertyStructure[1];
133: values[0] = value;
134: currentProperty.setValuesAsObject(values);
135: propertyAccessStrategy.updateProperty(request,
136: propertyIdentifier, rootProperty);
137: numberOfValues = 1;
138: }
139:
140: /* Do work : retrive property information and values */
141: Collection viewPropertyValues = new ArrayList();
142: for (int valueIndex = 0; valueIndex < numberOfValues; valueIndex++) {
143: Collection viewPropertiesList = getViewPropertiesList(
144: request, currentProperty, currentMetaProperty,
145: valueIndex);
146:
147: viewPropertyValues.add(new ViewPropertyValue(valueIndex,
148: viewPropertiesList));
149: }
150:
151: /* Create a view-object with property information for
152: the JSP wizard */
153: ViewPropertyInformation viewPropertyInformation = new ViewPropertyInformation(
154: propertySimpleName, viewPropertyValues);
155:
156: /* Do forward */
157: ActionForward forward = ((currentMetaProperty.isMultiValued()) ? mapping
158: .findForward("UpdateMultiValuedPropertyJSP")
159: : mapping.findForward("UpdateSingleValuedPropertyJSP"));
160: setParameters(request, frm, rootPropertyName,
161: propertyStructureName, viewPropertyInformation);
162:
163: return forward;
164: }
165:
166: /**
167: * Templete Method pattern hook. This method is used to retrive a
168: * <code>ViewProperty</code> list with information about a specific value
169: * of the current property. It will differently implemented depending on
170: * the property is simple or compound. Its assumed that this method returns
171: * view-properties ordered by its simple names.
172: * @param request the current request. Used if current propery is simple to
173: * get the message label <code>Prototypes.CommonLabels.value</code>
174: * @param property the property that is being updated
175: * @param metaProperty the property meta information
176: * @param valueIndex the value of this property that will be examined.
177: * @return a <code>Collection</code> of <code>ViewProperty</code> objects
178: * @throws <code>Exception</code> if an error ocurred
179: */
180: protected abstract Collection getViewPropertiesList(
181: HttpServletRequest request, Property property,
182: MetaProperty metaProperty, int valueIndex) throws Exception;
183:
184: /**
185: * Attaches parameters to the request for the forwarding to the JSP page.
186: * @param request the current request.
187: * @param frm the <code>UpdatePropertyForm</code> associated with this
188: * action.
189: * @param rootPropertyName the name of the root property that is being
190: * updated
191: * @param propertyStructureName the <code>propertyStructureName</code>
192: * of the current property.
193: * @param viewPropertyInformation a <code>ViewPropertyInformation</code>
194: * object describing the current property.
195: * @throws an Exception if an error happens
196: */
197: private void setParameters(HttpServletRequest request,
198: UpdatePropertyForm frm, String rootPropertyName,
199: String propertyStructureName,
200: ViewPropertyInformation viewPropertyInformation)
201: throws Exception {
202:
203: request.setAttribute("propertyInformation",
204: viewPropertyInformation);
205:
206: String propertyName = null;
207: if (propertyStructureName.equals("")) {
208: propertyName = rootPropertyName;
209: } else {
210: request.setAttribute("showReturnLink", "yes");
211: propertyName = rootPropertyName + "."
212: + propertyStructureName + "."
213: + viewPropertyInformation.getSimpleName();
214: }
215:
216: frm.setPropertyName(propertyName);
217: }
218: }
|