1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util.converter.function;
17
18 import java.io.FileNotFoundException;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.util.Properties;
22
23 import org.apache.struts.upload.FormFile;
24 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
25 import org.seasar.tuigwaa.database.util.FileData;
26 import org.seasar.tuigwaa.database.util.FileDataManager;
27 import org.seasar.tuigwaa.model.DataService;
28 import org.seasar.tuigwaa.model.common.EntityUtils;
29 import org.seasar.tuigwaa.model.core.impl.FileAttribute;
30 import org.seasar.tuigwaa.util.TgwNameUtils;
31
32 import com.isenshi.util.CharUtil;
33 import com.isenshi.util.SimpleFile;
34
35 public class FormFile2BytesConverterFunction extends ConverterFunction {
36
37 private DataService dataService = (DataService) SingletonS2ContainerFactory
38 .getContainer().getComponent(DataService.class);
39
40 public Object evaluate(Object obj) {
41 ConverterInfo info = new ConverterInfo(obj);
42
43 String src = getSourceField();
44 String target = getTargetField();
45
46 EntityUtils.setProperty(obj, target, info.getBytes());
47 EntityUtils.setProperty(obj, src, info.getFile());
48
49 return obj;
50 }
51
52 public Properties getExtraProperties() {
53 Properties props = new Properties();
54 props.put(FileAttribute.toFlag(getSourceField()), "java.lang.String");
55 props.put(FileAttribute.toUpdateFile(getSourceField()), FormFile.class
56 .getName());
57 return props;
58 }
59
60 class ConverterInfo {
61
62 Object obj;
63
64 FormFile file;
65
66 byte[] bytes;
67
68 boolean updateFlag;
69
70 boolean noupdateFlag;
71
72 boolean deleteFlag;
73
74 boolean newFlag;
75
76 String entityId;
77
78 String entityName = getEntity().getName();
79
80 String attrName = TgwNameUtils
81 .toPropertyName(getTargetField());
82
83 ConverterInfo(Object obj) {
84 this.obj = obj;
85 parseFlag();
86 parseFile();
87 }
88
89 public byte[] getBytes() {
90 return bytes;
91 }
92
93 public FormFile getFile() {
94 return file;
95 }
96
97 void parseFlag() {
98 String flag = FileAttribute.toFlag(getSourceField());
99 flag = (String) EntityUtils.getProperty(obj, flag);
100 updateFlag = FileAttribute.FLAG_UPDATE.equals(flag);
101 noupdateFlag = FileAttribute.FLAG_NOUPDATE.equals(flag);
102 deleteFlag = FileAttribute.FLAG_DELETE.equals(flag);
103 newFlag = !(noupdateFlag || updateFlag || deleteFlag);
104 }
105
106 void parseFile() {
107 Long id = EntityUtils.getEscapedId(obj);
108 entityId = "";
109 if(id != null){
110 entityId = "" + id;
111 }
112 if (newFlag) {
113 parseFile(getSourceField());
114 } else if (updateFlag) {
115 parseFile(FileAttribute.toUpdateFile(getSourceField()));
116 } else if (noupdateFlag) {
117 loadFile(obj);
118 } else if(deleteFlag){
119 FileDataManager.addFileData(new FileData(entityName, attrName, entityId, null));
120 }
121 }
122
123 void loadFile(Object obj) {
124 Long id = EntityUtils.getEscapedId(obj);
125 Object savedObj = dataService.load(getEntity(), id);
126 bytes = (byte[]) EntityUtils.getProperty(savedObj, TgwNameUtils
127 .removeEscapeCharacters(getTargetField()));
128 SimpleFile sfile = (SimpleFile) CharUtil.toObject(bytes);
129 file = new VirtualFormFile(sfile);
130 }
131
132 void parseFile(String src) {
133 file = (FormFile) EntityUtils.getProperty(obj, src);
134 if (file != null) {
135 try {
136 byte[] data = file.getFileData();
137 String contentType = file.getContentType();
138 String fileName = file.getFileName();
139 SimpleFile simpleFile = new SimpleFile(contentType,
140 fileName, null);
141 bytes = CharUtil.toBytes(simpleFile);
142 FileDataManager.addFileData(new FileData(entityName,
143 attrName, entityId, data));
144 } catch (FileNotFoundException e) {
145
146 } catch (IOException e) {
147
148 }
149 }
150 }
151 }
152
153 class VirtualFormFile implements FormFile {
154
155 private SimpleFile file;
156
157 public VirtualFormFile(SimpleFile file) {
158 this.file = file;
159 }
160
161 public String getFileName() {
162 return file.getFileName();
163 }
164
165 public String getContentType() {
166 return file.getContentType();
167 }
168
169 public int getFileSize() {
170 return 1;
171
172 }
173
174 public byte[] getFileData() throws FileNotFoundException, IOException {
175 return file.getData();
176 }
177
178 public void setContentType(String contentType) {
179 }
180
181 public void setFileSize(int fileSize) {
182 }
183
184 public void setFileName(String fileName) {
185 }
186
187 public InputStream getInputStream() throws FileNotFoundException,
188 IOException {
189 return null;
190 }
191
192 public void destroy() {
193 }
194 }
195 }