1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.control.form;
16
17 import org.figure8.join.control.JoinForm;
18 import org.figure8.join.control.action.DeliverableActions;
19 import org.figure8.join.services.cache.Cache;
20 import org.figure8.join.services.cache.EternalCacheKeys;
21 import org.figure8.join.services.cache.EternalCacheAccessor;
22 import org.figure8.join.businessobjects.artifact.DeliverableType;
23 import org.figure8.join.util.LogUtil;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.upload.FormFile;
28 import org.apache.struts.upload.MultipartRequestHandler;
29
30 import javax.servlet.http.HttpServletRequest;
31
32 import java.util.Date;
33 import java.text.SimpleDateFormat;
34 /**
35 * Struts form object used for manipulating deliverable business objects within Join.
36 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
37 * @version $Revision: 1.1 $
38 *
39 * @struts.form name="deliverableForm"
40 */
41 public class DeliverableForm extends JoinForm{
42
43
44
45 /** Get a commons logger. */
46 private static final Log log = LogUtil.getLog(DeliverableForm.class);
47
48
49
50
51 /** The deliverable key */
52 private String key;
53 /** The deliverable comments */
54 private String comments;
55 /** Deliverable version info */
56 private String versionInfo;
57 /** The deliverable creation date */
58 private Date creationDate;
59 /** The deliverable type key identifier */
60 private String typeKey;
61 /** The deliverable supplier identifier */
62 private String supplierId;
63 /** The name of release this deliverable is attached to */
64 private String releaseName;
65
66 /** String representation of <b>creationDate</b> */
67 private String creationDateStr;
68 /** File handler for deliverable content */
69 private FormFile deliverableFile;
70
71
72
73
74 /** Creates a new instance of DeliverableForm */
75 public DeliverableForm(){
76 }
77
78
79
80
81 /** @return This deliverable unique key */
82 public String getKey(){
83 return key;
84 }
85 /** @param key This deliverable unique key */
86 public void setKey(String key){
87 this.key = key;
88 }
89 /** @return The comments on this deliverable creation */
90 public String getComments(){
91 return comments;
92 }
93 /** @param comments The comments on this deliverable creation */
94 public void setComments(String comments){
95 this.comments = comments;
96 }
97 /** @return The version information for this deliverable */
98 public String getVersionInfo(){
99 return versionInfo;
100 }
101 /** @param versionInfo The version information for this deliverable */
102 public void setVersionInfo(String versionInfo){
103 this.versionInfo = versionInfo;
104 }
105 /** @return The creation date of this deliverable */
106 public Date getCreationDate(){
107 return creationDate;
108 }
109 /** @param creationDate The creation date of this message */
110 public void setCreationDate(Date creationDate){
111 this.creationDate = creationDate;
112
113 SimpleDateFormat format = new SimpleDateFormat(getMessageResources().getMessage("pattern.date.stringtodate"));
114 this.creationDateStr = format.format(creationDate);
115 }
116
117 /** @return This deliverable type unique key */
118 public String getTypeKey(){
119 return typeKey;
120 }
121 /** @param typeKey This deliverable type business key */
122 public void setTypeKey(String typeKey){
123 this.typeKey = typeKey;
124 }
125 /** @return The identifier of user that supplies this artifact */
126 public String getSupplierId(){
127 return supplierId;
128 }
129 /** @param supplierId The identifier of user that supplies this artifact */
130 public void setSupplierId(String supplierId){
131 this.supplierId = supplierId;
132 }
133 /** @return The name of the release this deliverable is attached to */
134 public String getReleaseName(){
135 return releaseName;
136 }
137 /** @param releaseName The name of release this deliverable is attached to */
138 public void setReleaseName(String releaseName){
139 this.releaseName = releaseName;
140 }
141
142 /** @return Creation date for this deliverable (should be a date) */
143 public String getCreationDateStr(){
144 return creationDateStr;
145 }
146 /** @param creationDateStr Creation date of this deliverable (should be a date) */
147 public void setCreationDateStr(String creationDateStr){
148 this.creationDateStr = creationDateStr;
149 }
150
151 /** @return The uploaded deliverable file */
152 public FormFile getDeliverableFile(){
153 return deliverableFile;
154 }
155 /** @param deliverableFile The uploaded deliverable file */
156 public void setDeliverableFile(FormFile deliverableFile){
157 this.deliverableFile = deliverableFile;
158 }
159
160
161
162
163 /**
164 * Validation of the form attributes.
165 * @param operation String representing the operation to invoke on Action
166 * @param mapping Mapping between forwards name and path for this action
167 * @param request The servlet container request wrapper
168 */
169 public void doValidate(String operation, ActionMapping mapping, HttpServletRequest request){
170
171 if (DeliverableActions.LOAD_OP.equals(operation) || DeliverableActions.DETAILS_OP.equals(operation)){
172
173 if (key == null || key.length() == 0)
174 addActionError("errors.required", getGuiMessageResources().getMessage("label.deliverable.key"));
175 }
176 else if (DeliverableActions.FORM_OP.equals(operation)){
177
178 if (typeKey == null || typeKey.length() == 0)
179 addActionError("errors.required", getGuiMessageResources().getMessage("label.deliverable.type"));
180 }
181 else if (DeliverableActions.SAVE_OP.equals(operation) || DeliverableActions.CREATE_OP.equals(operation)){
182
183 if (comments == null || comments.length() == 0)
184 addActionError("errors.required", getGuiMessageResources().getMessage("label.deliverable.comments"));
185
186 if (versionInfo == null || versionInfo.length() == 0)
187 addActionError("errors.required", getGuiMessageResources().getMessage("label.deliverable.version"));
188
189 if (releaseName == null || releaseName.length() == 0)
190 addActionError("errors.required", getGuiMessageResources().getMessage("label.deliverable.release"));
191
192 if (typeKey == null || typeKey.length() == 0)
193 addActionError("errors.required", getGuiMessageResources().getMessage("label.deliverable.type"));
194 }
195
196 if (DeliverableActions.CREATE_OP.equals(operation)){
197 if (typeKey != null){
198
199 Cache typesCache = getDeliverableTypeKeyToDeliverableTypeCache();
200 if (typesCache == null){
201
202 log.error("Application is in illegal state : DeliverableType cache is not present in context");
203 throw new IllegalStateException("Application is in illegal state : DeliverableType cache is not present in context");
204 }
205
206 DeliverableType type = (DeliverableType)typesCache.get(typeKey);
207 if (type != null && !type.getVcsDeliverable()){
208
209 if (deliverableFile == null)
210 addActionError("errors.required", getGuiMessageResources().getMessage("label.deliverable.file"));
211 else{
212
213 Boolean maxlength = (Boolean)request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
214 if ((maxlength != null) && (maxlength.booleanValue()))
215 addActionError("errors.filesize", deliverableFile.getFileName());
216 }
217 }
218 }
219 }
220 }
221
222
223
224
225 /**
226 * Reset form attributes.
227 * @param mapping Mapping between forwards name and path for this action
228 * @param request The servlet container request wrapper
229 */
230 public void reset(ActionMapping mapping, HttpServletRequest request){
231 super.reset(mapping, request);
232 this.key = null;
233 this.comments = null;
234 this.versionInfo = null;
235 this.creationDate = null;
236 this.supplierId = null;
237 this.releaseName = null;
238 this.creationDateStr = null;
239 this.deliverableFile = null;
240 }
241
242
243
244
245 /** Get the cache for deliverable types or null if it is not available */
246 private Cache getDeliverableTypeKeyToDeliverableTypeCache(){
247
248 EternalCacheAccessor cacheAccessor = (EternalCacheAccessor)servlet.getServletContext().getAttribute(
249 EternalCacheAccessor.CONTEXT_KEY);
250
251 if (cacheAccessor != null){
252 return cacheAccessor.getCache(EternalCacheKeys.DELTYPE_KEY_TO_DELTYPE_KEY);
253 }
254 return null;
255 }
256 }