001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: SubmissionBuilder.java 3701 2007-03-18 12:24:23Z gbevin $
007: */
008: package com.uwyn.rife.engine;
009:
010: import com.uwyn.rife.engine.BeanDeclaration;
011: import java.util.ArrayList;
012: import java.util.LinkedHashMap;
013: import java.util.Map;
014:
015: public class SubmissionBuilder {
016: private ElementInfoBuilder mElementInfoBuilder = null;
017: private String mName = null;
018:
019: private LinkedHashMap<String, String[]> mParameters = null;
020: private ArrayList<String> mParametersRegexp = null;
021: private LinkedHashMap<BeanDeclaration, String> mBeans = null;
022: private ArrayList<String> mFiles = null;
023: private ArrayList<String> mFilesRegexp = null;
024: private Scope mScope = null;
025: private boolean mCancelContinuations = false;
026:
027: SubmissionBuilder(ElementInfoBuilder elementInfoBuilder, String name) {
028: assert elementInfoBuilder != null;
029: assert name != null;
030:
031: mElementInfoBuilder = elementInfoBuilder;
032: mName = name;
033:
034: return;
035: }
036:
037: Submission getSubmission(ElementInfo elementInfo) {
038: Submission submission = new Submission();
039: elementInfo.addSubmission(mName, submission);
040: submission.setCancelContinuations(mCancelContinuations);
041: submission.setScope(mScope);
042:
043: if (mParameters != null) {
044: for (Map.Entry<String, String[]> parameter : mParameters
045: .entrySet()) {
046: submission.addParameter(parameter.getKey(), parameter
047: .getValue());
048: }
049: }
050:
051: if (mParametersRegexp != null) {
052: for (String pattern : mParametersRegexp) {
053: submission.addParameterRegexp(pattern);
054: }
055: }
056:
057: if (mBeans != null) {
058: for (Map.Entry<BeanDeclaration, String> bean : mBeans
059: .entrySet()) {
060: submission.addBean(bean.getKey(), bean.getValue());
061: }
062: }
063:
064: if (mFiles != null) {
065: for (String name : mFiles) {
066: submission.addFile(name);
067: }
068: }
069:
070: if (mFilesRegexp != null) {
071: for (String pattern : mFilesRegexp) {
072: submission.addFileRegexp(pattern);
073: }
074: }
075:
076: return submission;
077: }
078:
079: public SubmissionBuilder cancelContinuations(
080: boolean cancelContinuations) {
081: mCancelContinuations = cancelContinuations;
082: return this ;
083: }
084:
085: public SubmissionBuilder setScope(Scope scope) {
086: mScope = scope;
087:
088: return this ;
089: }
090:
091: public SubmissionBuilder addParameter(String name) {
092: return addParameter(name, null);
093: }
094:
095: public SubmissionBuilder addParameter(String name,
096: String[] defaultValues) {
097: if (null == mParameters) {
098: mParameters = new LinkedHashMap<String, String[]>();
099: }
100:
101: mParameters.put(name, defaultValues);
102:
103: return this ;
104: }
105:
106: public SubmissionBuilder addParameterRegexp(String pattern) {
107: if (null == mParametersRegexp) {
108: mParametersRegexp = new ArrayList<String>();
109: }
110:
111: mParametersRegexp.add(pattern);
112:
113: return this ;
114: }
115:
116: public SubmissionBuilder addBean(String classname) {
117: return addBean(classname, null, null, null);
118: }
119:
120: public SubmissionBuilder addBean(String classname, String prefix) {
121: return addBean(classname, prefix, null, null);
122: }
123:
124: public SubmissionBuilder addBean(String classname, String prefix,
125: String name) {
126: return addBean(classname, prefix, name, null);
127: }
128:
129: public SubmissionBuilder addBean(String classname, String prefix,
130: String name, String groupName) {
131: if (prefix != null && 0 == prefix.length())
132: prefix = null;
133: if (name != null && 0 == name.length())
134: name = null;
135: if (groupName != null && 0 == groupName.length())
136: groupName = null;
137:
138: if (null == mBeans) {
139: mBeans = new LinkedHashMap<BeanDeclaration, String>();
140: }
141:
142: mBeans.put(new BeanDeclaration(classname, prefix, groupName),
143: name);
144:
145: return this ;
146: }
147:
148: public SubmissionBuilder addBean(Class klass) {
149: return addBean(klass, null, null, null);
150: }
151:
152: public SubmissionBuilder addBean(Class klass, String prefix) {
153: return addBean(klass, prefix, null, null);
154: }
155:
156: public SubmissionBuilder addBean(Class klass, String prefix,
157: String name) {
158: return addBean(klass, prefix, name, null);
159: }
160:
161: public SubmissionBuilder addBean(Class klass, String prefix,
162: String name, String groupName) {
163: return addBean(new BeanDeclaration(klass, prefix, groupName),
164: name);
165: }
166:
167: private SubmissionBuilder addBean(BeanDeclaration beanDeclaration,
168: String name) {
169: if (null == mBeans) {
170: mBeans = new LinkedHashMap<BeanDeclaration, String>();
171: }
172:
173: mBeans.put(beanDeclaration, name);
174:
175: return this ;
176: }
177:
178: public SubmissionBuilder addFile(String name) {
179: if (null == mFiles) {
180: mFiles = new ArrayList<String>();
181: }
182:
183: mFiles.add(name);
184:
185: return this ;
186: }
187:
188: public SubmissionBuilder addFileRegexp(String pattern) {
189: if (null == mFilesRegexp) {
190: mFilesRegexp = new ArrayList<String>();
191: }
192:
193: mFilesRegexp.add(pattern);
194:
195: return this ;
196: }
197:
198: public ElementInfoBuilder leaveSubmission() {
199: return mElementInfoBuilder;
200: }
201: }
|