001: /*
002: * RequestUtils.java
003: *
004: * Version: $Revision: 1.0 $
005: *
006: * Date: $Date: 2006/07/13 23:20:54 $
007: *
008: * Copyright (c) 2002, Hewlett-Packard Company and Massachusetts
009: * Institute of Technology. All rights reserved.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions are
013: * met:
014: *
015: * - Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * - Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in the
020: * documentation and/or other materials provided with the distribution.
021: *
022: * - Neither the name of the Hewlett-Packard Company nor the name of the
023: * Massachusetts Institute of Technology nor the names of their
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
028: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
030: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
032: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
033: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
034: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
035: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
036: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
037: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
038: * DAMAGE.
039: */
040:
041: package org.dspace.app.xmlui.utils;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: import org.apache.cocoon.environment.Request;
047:
048: /**
049: * General utilities for common operations on Request objects.
050: *
051: * @author Scott Phillips
052: */
053:
054: public class RequestUtils {
055:
056: /**
057: * Get values for a field from a DRI multi value field, since a field may
058: * have multiple values this method will check for fieldName + "_n" untill
059: * it does not find any more values. The result is a list of all values
060: * for a field.
061: *
062: * If the value has been seleted to be removed then it is removed from
063: * the list.
064: *
065: * @param request
066: * the request containing the form information
067: * @param compositeFieldName
068: * The fieldName of the composite field.
069: * @param componentFieldName
070: * The fieldName of the component field
071: * @return a List of Strings
072: */
073: public static List<String> getCompositeFieldValues(Request request,
074: String compositeFieldName, String componentFieldName) {
075: List<String> values = new ArrayList<String>();
076:
077: int i = -1;
078:
079: // Iterate through the values in the form.
080: valueLoop: while (1 == 1) {
081: i++;
082: String value = null;
083:
084: // Get the component field's name
085: if (i == 0)
086: value = request.getParameter(componentFieldName);
087: else
088: value = request.getParameter(componentFieldName + "_"
089: + i);
090:
091: // If this is null then it's the last one.
092: if (value == null)
093: break valueLoop;
094:
095: // Check to make sure that this value is not selected to be removed.
096: String[] selected = request
097: .getParameterValues(compositeFieldName
098: + "_selected");
099:
100: if (selected != null) {
101: for (String select : selected) {
102: if (select.equals(compositeFieldName + "_" + i)) {
103: // Found, the user has requested that this value be deleted.
104: continue valueLoop;
105: }
106: }
107: }
108:
109: // Only add non blank items to the list
110: if (value.length() == 0)
111: continue valueLoop;
112:
113: // Finaly add it to the list
114: values.add(value.trim());
115: }
116:
117: return values;
118: }
119:
120: /**
121: * Get values from a DRI multi value field, since a field may have multiple
122: * values this method will check for fieldName + "_n" untill it does not
123: * find any more values. The result is a list of all values for a field.
124: *
125: * If the value has been seleted to be removed then it is removed from
126: * the list.
127: *
128: * @param request
129: * the request containing the form information
130: * @param fieldName
131: * The fieldName of the composite field.
132: * @return a List of Strings
133: */
134: public static List<String> getFieldValues(Request request,
135: String fieldName) {
136: List<String> values = new ArrayList<String>();
137:
138: int i = -1;
139:
140: // Iterate through the values in the form.
141: valueLoop: while (1 == 1) {
142: i++;
143: String value = null;
144:
145: // Get the component field's name
146: if (i == 0)
147: value = request.getParameter(fieldName);
148: else
149: value = request.getParameter(fieldName + "_" + i);
150:
151: // If this is null then it's the last one.
152: if (value == null)
153: break valueLoop;
154:
155: // Check to make sure that this value is not selected to be removed.
156: String[] selected = request.getParameterValues(fieldName
157: + "_selected");
158:
159: if (selected != null) {
160: for (String select : selected) {
161: if (select.equals(fieldName + "_" + i)) {
162: // Found, the user has requested that this value be deleted.
163: continue valueLoop;
164: }
165: }
166: }
167:
168: // Only add non blank items to the list
169: if (value.length() == 0)
170: continue valueLoop;
171:
172: // Finaly add it to the list
173: values.add(value.trim());
174: }
175:
176: return values;
177: }
178:
179: /**
180: * Obtain a parameter from the given request as an int. <code>-1</code> is
181: * returned if the parameter is garbled or does not exist.
182: *
183: * @param request
184: * the HTTP request
185: * @param param
186: * the name of the parameter
187: *
188: * @return the integer value of the parameter, or -1
189: */
190: public static int getIntParameter(Request request, String param) {
191: String val = request.getParameter(param);
192:
193: try {
194: return Integer.parseInt(val.trim());
195: } catch (Exception e) {
196: // Problem with parameter
197: return -1;
198: }
199: }
200:
201: }
|