01: package org.mdarad.framework.util.struts;
02:
03: import java.io.FileNotFoundException;
04: import java.io.IOException;
05:
06: import org.apache.struts.upload.FormFile;
07: import org.dataisland.primitives.datatype.Blob;
08:
09: public class PrimitiveToStrutsBeanFormTypeMappings {
10:
11: public static Object getEntityPropertyValueFromStrutsBeanPropertyValue(
12: String dataislandPrimitiveName, Object oldPOJOValue,
13: Object newFormValue) {
14: Object output = null;
15: if ("blob".equals(dataislandPrimitiveName)) {
16:
17: output = getBlobValue((Blob) oldPOJOValue,
18: (FormFile) newFormValue);
19:
20: } else if ("percentage".equals(dataislandPrimitiveName)) {
21: Float formFloat = (Float) newFormValue;
22:
23: if (formFloat != null) {
24: output = new Float(formFloat.floatValue() / 100);
25: }
26: } else {
27: output = newFormValue;
28: }
29: return output;
30: }
31:
32: public static Blob getBlobValue(Blob oldPOJOValue,
33: FormFile newFormValue) {
34: Blob output = null;
35:
36: if (newFormValue == null) {
37: output = oldPOJOValue;
38: } else {
39: FormFile formFile = (FormFile) newFormValue;
40: if (formFile != null && formFile.getFileSize() > 0) {
41: try {
42: Blob blob = new Blob();
43: blob.setContent(formFile.getFileData());
44: blob.setContentType(formFile.getContentType());
45: blob.setFileName(formFile.getFileName());
46: output = blob;
47: } catch (FileNotFoundException e) {
48: // TODO Auto-generated catch block
49: e.printStackTrace();
50: } catch (IOException e) {
51: // TODO Auto-generated catch block
52: e.printStackTrace();
53: }
54: }
55: }
56: return output;
57: }
58:
59: public static Object getStrutsBeanPropertyValueFromEntityPropertyValue(
60: String dataislandPrimitiveName, Object currentPOJOValue) {
61: Object output = null;
62:
63: if ("blob".equals(dataislandPrimitiveName)) {
64: // do nothing (blob is never loaded into form)
65: } else if ("percentage".equals(dataislandPrimitiveName)) {
66: Float beanFloat = (Float) currentPOJOValue;
67:
68: if (beanFloat != null) {
69: output = new Float(beanFloat.floatValue() * 100);
70: }
71: } else {
72: output = currentPOJOValue;
73: }
74:
75: return output;
76: }
77:
78: }
|