1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
package pt.digitalis.dif.dem.objects.parameters.types; |
6 | |
|
7 | |
import java.awt.image.BufferedImage; |
8 | |
import java.io.ByteArrayInputStream; |
9 | |
import java.io.ByteArrayOutputStream; |
10 | |
import java.io.IOException; |
11 | |
import java.math.BigDecimal; |
12 | |
import java.util.ArrayList; |
13 | |
import java.util.Date; |
14 | |
import java.util.List; |
15 | |
|
16 | |
import javax.imageio.ImageIO; |
17 | |
|
18 | |
import org.imgscalr.Scalr; |
19 | |
|
20 | |
import pt.digitalis.dif.controller.http.HTTPConstants; |
21 | |
import pt.digitalis.dif.controller.interfaces.IDIFContext; |
22 | |
import pt.digitalis.dif.controller.objects.DIFSessionConstants; |
23 | |
import pt.digitalis.dif.dem.interfaces.IStageInstance; |
24 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterError; |
25 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrorList; |
26 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrorType; |
27 | |
import pt.digitalis.dif.exception.objects.ParameterException; |
28 | |
import pt.digitalis.dif.ioc.DIFIoCRegistry; |
29 | |
import pt.digitalis.dif.utils.extensions.document.DocumentRepositoryEntry; |
30 | |
import pt.digitalis.dif.utils.extensions.document.DocumentRepositoryException; |
31 | |
import pt.digitalis.dif.utils.extensions.document.IDocumentRepositoryManager; |
32 | |
import pt.digitalis.dif.utils.logging.DIFLogger; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class DocumentParameter extends AbstractParameter<DocumentRepositoryEntry> { |
43 | |
|
44 | |
|
45 | |
private static final String DOCUMENT_ID_HIDDEN_FIELD = "_docid"; |
46 | |
|
47 | |
@SuppressWarnings("serial") |
48 | 0 | final static private List<String> supportedClasses = new ArrayList<String>() { |
49 | |
|
50 | |
{ |
51 | 0 | add(DocumentRepositoryEntry.class.getCanonicalName()); |
52 | |
} |
53 | |
}; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public List<String> getSupportedClasses() |
59 | |
{ |
60 | 0 | return supportedClasses; |
61 | |
} |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public BigDecimal getValueAsBigDecimal(IDIFContext context) throws ParameterException |
67 | |
{ |
68 | |
|
69 | 0 | throw new ParameterException(getMessages().getMessages(context.getLanguage()).get("invalidUsage"), this); |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public boolean getValueAsBoolean(IDIFContext context) throws ParameterException |
76 | |
{ |
77 | |
|
78 | 0 | throw new ParameterException(getMessages().getMessages(context.getLanguage()).get("invalidUsage"), this); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public Date getValueAsDate(IDIFContext context) throws ParameterException |
85 | |
{ |
86 | |
|
87 | 0 | throw new ParameterException(getMessages().getMessages(context.getLanguage()).get("invalidUsage"), this); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@Override |
94 | |
public DocumentRepositoryEntry getValueAsDocument(IDIFContext context) throws ParameterException |
95 | |
{ |
96 | 0 | return getValue(context); |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public Double getValueAsDouble(IDIFContext context) throws ParameterException |
103 | |
{ |
104 | |
|
105 | 0 | throw new ParameterException(getMessages().getMessages(context.getLanguage()).get("invalidUsage"), this); |
106 | |
} |
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public Long getValueAsLong(IDIFContext context) throws ParameterException |
112 | |
{ |
113 | |
|
114 | 0 | throw new ParameterException(getMessages().getMessages(context.getLanguage()).get("invalidUsage"), this); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
@Override |
121 | |
public String getValueAsString(IDIFContext context) throws ParameterException |
122 | |
{ |
123 | |
|
124 | 0 | throw new ParameterException(getMessages().getMessages(context.getLanguage()).get("invalidUsage"), this); |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public boolean isNumeric() |
131 | |
{ |
132 | 0 | return false; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
@Override |
139 | |
public boolean isStringSetterSupported() |
140 | |
{ |
141 | 0 | return false; |
142 | |
} |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
@Override |
148 | |
public ParameterErrorList refreshParameterValue(IStageInstance stageInstance) |
149 | |
{ |
150 | 0 | ParameterErrorList errorList = super.refreshParameterValue(stageInstance); |
151 | |
|
152 | 0 | if (!this.hasValue()) |
153 | |
{ |
154 | 0 | Object previousDocumentID = stageInstance.getContext().getRequest() |
155 | 0 | .getParameter(this.getId() + DOCUMENT_ID_HIDDEN_FIELD); |
156 | 0 | if (previousDocumentID != null) |
157 | |
{ |
158 | |
try |
159 | |
{ |
160 | 0 | this.setValue(DIFIoCRegistry.getRegistry().getImplementation(IDocumentRepositoryManager.class) |
161 | 0 | .getDocument(Long.parseLong(previousDocumentID.toString())), stageInstance); |
162 | 0 | if (hasValue()) |
163 | |
{ |
164 | 0 | for (ParameterError error: errorList.getErrorList()) |
165 | |
{ |
166 | 0 | if (error.getErrorType() == ParameterErrorType.MISSING) |
167 | |
{ |
168 | 0 | errorList.getErrorList().remove(error); |
169 | 0 | break; |
170 | |
} |
171 | |
} |
172 | |
} |
173 | |
} |
174 | 0 | catch (Exception e) |
175 | |
{ |
176 | |
|
177 | |
|
178 | 0 | e.printStackTrace(); |
179 | |
} |
180 | |
} |
181 | |
} |
182 | |
|
183 | 0 | if (stageInstance.getContext().getRequest().getAttribute(HTTPConstants.UPLOAD_FILE_SIZE_ERROR) != null) |
184 | |
{ |
185 | 0 | ParameterError pError = new ParameterError( |
186 | 0 | getMessages().getMessages(stageInstance.getContext().getLanguage()).get("fileUploadMaxSize") |
187 | 0 | + " " |
188 | 0 | + stageInstance.getContext().getSession() |
189 | 0 | .getAttribute(DIFSessionConstants.MAX_DOCUMENT_SIZE), ParameterErrorType.OTHER); |
190 | 0 | pError.setForceShowError(true); |
191 | |
|
192 | 0 | errorList.addError(pError); |
193 | |
|
194 | |
|
195 | 0 | stageInstance.getContext().getRequest().addAttribute(HTTPConstants.UPLOAD_FILE_SIZE_ERROR, null); |
196 | |
} |
197 | |
|
198 | 0 | return errorList; |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
@Override |
206 | |
public ParameterErrorList setValue(DocumentRepositoryEntry value, IStageInstance stageInstance, |
207 | |
boolean initializationInProgress) |
208 | |
{ |
209 | |
|
210 | 0 | if (value != null && value.getBytes() != null |
211 | 0 | && stageInstance.getContext().getRequest().getParameter(this.getId() + "_resize") != null) |
212 | |
{ |
213 | |
|
214 | 0 | String resizeParams[] = ((String) stageInstance.getContext().getRequest() |
215 | 0 | .getParameter(this.getId() + "_resize")).split("x"); |
216 | 0 | Integer width = Integer.parseInt(resizeParams[0]); |
217 | 0 | Integer height = resizeParams.length > 1 ? Integer.parseInt(resizeParams[1]) : width; |
218 | |
|
219 | 0 | ByteArrayInputStream bais = new ByteArrayInputStream(value.getBytes()); |
220 | |
BufferedImage image; |
221 | |
try |
222 | |
{ |
223 | 0 | image = ImageIO.read(bais); |
224 | 0 | bais.close(); |
225 | |
|
226 | 0 | if (image.getHeight() > height || image.getWidth() > width) |
227 | |
{ |
228 | |
BufferedImage resizedImage; |
229 | |
|
230 | 0 | resizedImage = Scalr.resize(image, Scalr.Method.AUTOMATIC, Scalr.Mode.AUTOMATIC, width, height); |
231 | |
|
232 | |
|
233 | 0 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
234 | 0 | ImageIO.write(resizedImage, value.getMimeType(), baos); |
235 | 0 | baos.flush(); |
236 | 0 | value.setBytes(baos.toByteArray()); |
237 | 0 | baos.close(); |
238 | |
} |
239 | |
} |
240 | 0 | catch (IOException e) |
241 | |
{ |
242 | 0 | DIFLogger.getLogger().warn("Could not resize the image: " + value.getFileName()); |
243 | 0 | e.printStackTrace(); |
244 | |
} |
245 | |
} |
246 | |
|
247 | |
|
248 | 0 | if (stageInstance.getContext().getRequest().getParameter(this.getId() + "_delete") != null) |
249 | |
{ |
250 | 0 | Object docID = stageInstance.getContext().getRequest() |
251 | 0 | .getParameter(this.getId() + DOCUMENT_ID_HIDDEN_FIELD); |
252 | |
|
253 | 0 | if (docID != null) |
254 | |
{ |
255 | |
|
256 | |
|
257 | 0 | value = new DocumentRepositoryEntry(); |
258 | 0 | value.setId(Long.parseLong(docID.toString())); |
259 | |
|
260 | |
try |
261 | |
{ |
262 | 0 | value = DIFIoCRegistry.getRegistry().getImplementation(IDocumentRepositoryManager.class) |
263 | 0 | .getDocument(Long.parseLong(docID.toString())); |
264 | |
} |
265 | 0 | catch (NumberFormatException e) |
266 | |
{ |
267 | 0 | e.printStackTrace(); |
268 | |
} |
269 | 0 | catch (DocumentRepositoryException e) |
270 | |
{ |
271 | 0 | e.printStackTrace(); |
272 | |
} |
273 | |
|
274 | 0 | if (value != null) |
275 | 0 | value.setDeleteRequested(true); |
276 | |
} |
277 | |
} |
278 | |
|
279 | 0 | return super.setValue(value, stageInstance, initializationInProgress); |
280 | |
} |
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public ParameterErrorList setValueFromString(String value, IStageInstance stageInstance, |
287 | |
boolean initializationInProgress) |
288 | |
{ |
289 | 0 | ParameterErrorList list = new ParameterErrorList(this, value); |
290 | 0 | ParameterError error = new ParameterError(getMessages().getMessages(stageInstance.getContext().getLanguage()) |
291 | 0 | .get("invalidUsage"), ParameterErrorType.OTHER); |
292 | 0 | list.addError(error); |
293 | |
|
294 | 0 | return list; |
295 | |
} |
296 | |
} |