001: package com.technoetic.xplanner.forms;
002:
003: import java.text.SimpleDateFormat;
004: import java.text.ParseException;
005: import java.text.Format;
006: import java.text.NumberFormat;
007: import java.util.ArrayList;
008: import java.util.Date;
009: import javax.servlet.http.HttpServletRequest;
010:
011: import org.apache.struts.Globals;
012: import org.apache.struts.action.ActionError;
013: import org.apache.struts.action.ActionErrors;
014: import org.apache.struts.action.ActionForm;
015: import org.apache.struts.action.ActionMapping;
016: import org.apache.struts.upload.FormFile;
017: import org.apache.struts.util.MessageResources;
018:
019: import com.technoetic.xplanner.format.DecimalFormat;
020:
021: public abstract class AbstractEditorForm extends ActionForm {
022: private String action;
023: private String oid;
024: private boolean merge;
025: private int id;
026:
027: protected static SimpleDateFormat dateTimeConverter;
028: protected static SimpleDateFormat dateConverter;
029: protected static DecimalFormat decimalConverter;
030: protected static NumberFormat numberConverter;
031:
032: public void reset(ActionMapping mapping, HttpServletRequest request) {
033: super .reset(mapping, request);
034: action = null;
035: oid = null;
036: merge = false;
037: id = 0;
038: }
039:
040: public String getAction() {
041: return action;
042: }
043:
044: public void setAction(String action) {
045: this .action = action;
046: }
047:
048: public boolean isSubmitted() {
049: return isPresent(action);
050: }
051:
052: public String getMode() {
053: return oid == null ? "create" : "modify";
054: }
055:
056: public void setOid(String oid) {
057: this .oid = oid;
058: }
059:
060: public String getOid() {
061: return oid;
062: }
063:
064: public static void error(ActionErrors errors, String key) {
065: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(key));
066: }
067:
068: public static void error(ActionErrors errors, String key,
069: Object[] values) {
070: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(key,
071: values));
072: }
073:
074: public static boolean isPresent(String value) {
075: return value != null && !value.equals("")
076: && !value.equals("null");
077: }
078:
079: public static void require(ActionErrors errors, String value,
080: String msgkey) {
081: if (!isPresent(value)) {
082: error(errors, msgkey);
083: }
084: }
085:
086: public static void require(ActionErrors errors, Date value,
087: String msgkey) {
088: if (value.getTime() == 0) {
089: error(errors, msgkey);
090: }
091: }
092:
093: public static void require(ActionErrors errors, int value,
094: String msgkey) {
095: if (value == 0) {
096: error(errors, msgkey);
097: }
098: }
099:
100: public static void require(ActionErrors errors, boolean valid,
101: String msgkey) {
102: if (!valid) {
103: error(errors, msgkey);
104: }
105: }
106:
107: public static void require(ActionErrors errors, FormFile value,
108: String msgkey) {
109: if (value == null || value.getFileSize() == 0) {
110: error(errors, msgkey);
111: }
112: }
113:
114: public static void require(ActionErrors errors, byte[] value,
115: String msgkey) {
116: if (value == null || value.length == 0) {
117: error(errors, msgkey);
118: }
119: }
120:
121: public void setMerge(boolean merge) {
122: this .merge = merge;
123: }
124:
125: public boolean isMerge() {
126: return merge;
127: }
128:
129: public void setId(int id) {
130: this .id = id;
131: }
132:
133: public int getId() {
134: return id;
135: }
136:
137: public static MessageResources getResources(
138: HttpServletRequest request) {
139: return (MessageResources) request
140: .getAttribute(Globals.MESSAGES_KEY);
141: }
142:
143: public static void ensureSize(ArrayList list, int size) {
144: if (size > list.size()) {
145: for (int i = list.size(); i < size; i++) {
146: list.add(null);
147: }
148: }
149: }
150:
151: public static void initConverters(HttpServletRequest request) {
152: if (dateTimeConverter == null) {
153: String format = getResources(request).getMessage(
154: "format.datetime");
155: dateTimeConverter = new SimpleDateFormat(format);
156: }
157:
158: if (dateConverter == null) {
159: String format = getResources(request).getMessage(
160: "format.date");
161: dateConverter = new SimpleDateFormat(format);
162: }
163:
164: if (decimalConverter == null) {
165: decimalConverter = new DecimalFormat(request);
166: }
167:
168: if (numberConverter == null) {
169: numberConverter = NumberFormat.getIntegerInstance();
170: }
171: }
172:
173: public static Date convertToDate(String date,
174: String errorMessageKey, ActionErrors errors) {
175: return (Date) convert(date, errorMessageKey, errors,
176: dateConverter);
177: }
178:
179: public static Date convertToDateTime(String date,
180: String errorMessageKey, ActionErrors errors) {
181: return (Date) convert(date, errorMessageKey, errors,
182: dateTimeConverter);
183: }
184:
185: public static Number convertToInt(String integer,
186: String errorMessageKey, ActionErrors errors) {
187: return (Number) convert(integer, errorMessageKey, errors,
188: numberConverter);
189: }
190:
191: public static Number convertToInt(String integer) {
192: return (Number) convert(integer, numberConverter);
193: }
194:
195: public static Object convert(String stringValue, Format format) {
196: if (isPresent(stringValue))
197: try {
198: return format.parseObject(stringValue);
199: } catch (ParseException ex) {
200: //don't do anything
201: }
202: return null;
203: }
204:
205: public static Object convert(String stringValue,
206: String errorMessageKey, ActionErrors errors, Format format) {
207: Object o = convert(stringValue, format);
208: if (o == null && isPresent(stringValue))
209: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
210: errorMessageKey));
211: return o;
212: }
213: }
|