001: /**
002: * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017: * USA
018: *
019: * Component of: Red Hat Application Server
020: *
021: * Initial Developers: Aizaz Ahmed
022: * Vivek Lakshmanan
023: * Andrew Overholt
024: * Matthew Wringe
025: *
026: */package olstore.action;
027:
028: import java.util.Vector;
029:
030: import javax.servlet.http.*;
031:
032: import olstore.dto.PropertyValue;
033: import olstore.form.CreateTypeForm;
034:
035: import org.apache.struts.action.*;
036:
037: public class TypeCreateAction extends DemoBaseAction {
038:
039: /**
040: * Acts as a relay for all item related tasks with a default action
041: *
042: */
043: public ActionForward execute(ActionMapping mapping,
044: ActionForm form, HttpServletRequest request,
045: HttpServletResponse response) throws Exception {
046:
047: //Throw an error if it doesn't equal of any of these?
048: try {
049: String action = ((CreateTypeForm) form).getSubmitType();
050: if (action == null || action.equals("")) {
051: CreateNewType(mapping, form, request, response);
052: } else if (action.equals("updateProperties")) {
053: updateProperties(mapping, form, request, response);
054: } else if (action.equals("saveType")) {
055: return mapping.findForward("saveType");
056: }
057:
058: return mapping.findForward("updateType");
059:
060: } catch (Exception e) {
061: //Logging code here
062: ActionErrors errors = new ActionErrors();
063: errors.add("error", new ActionError("errors.type.load", e
064: .getMessage()));
065: saveErrors(request, errors);
066: // Return to same page
067: return (new ActionForward(mapping.getInput()));
068: }
069:
070: }
071:
072: public void CreateNewType(ActionMapping mapping, ActionForm form,
073: HttpServletRequest request, HttpServletResponse response)
074: throws Exception {
075:
076: // Add a few empty Properties as defaults
077: Vector props = new Vector();
078: for (int i = 0; i < 5; i++) {
079: props.add(new PropertyValue());
080: }
081: ((CreateTypeForm) form).setProperties(props);
082: }
083:
084: /**
085: * This method updates the number of properties
086: * for display. The only validation that should take place in this
087: * particular action is that the total number of fields is a valid
088: * integer
089: */
090: public ActionForward updateProperties(ActionMapping mapping,
091: ActionForm form, HttpServletRequest request,
092: HttpServletResponse response) throws Exception {
093: try {
094: Vector propValueArr = ((CreateTypeForm) form)
095: .getProperties();
096: String numPropsStr = ((CreateTypeForm) form).getNumProps();
097: int numProps = Integer.parseInt(numPropsStr);
098:
099: // if the number is smaller than the present length, then truncate
100: // the existing array
101: if (numProps < propValueArr.size()) {
102: for (int i = propValueArr.size() - 1; i > numProps - 1; i--) {
103: propValueArr.remove(i);
104: }
105: }
106:
107: // If the number is larger, then grow the ArrayList
108: if (numProps > propValueArr.size()) {
109: for (int i = propValueArr.size(); i < numProps; i++) {
110: propValueArr.add(new PropertyValue());
111: }
112: }
113:
114: ((CreateTypeForm) form).setProperties(propValueArr);
115: return mapping.findForward("updateType");
116: }
117:
118: catch (NumberFormatException n) {
119: ActionErrors errors = new ActionErrors();
120: errors.add("error", new ActionError("errors.not.int", n
121: .getMessage()));
122: saveErrors(request, errors);
123: return (new ActionForward(mapping.getInput()));
124: }
125:
126: catch (Exception e) {
127: //Logging code here
128: ActionErrors errors = new ActionErrors();
129: errors.add("error", new ActionError("errors.item.load", e
130: .getMessage()));
131: saveErrors(request, errors);
132: // Return to same page
133: return (new ActionForward(mapping.getInput()));
134: }
135:
136: }
137:
138: }
|