View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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 					// do nothing
146 				} catch (IOException e) {
147 					// do nothing
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 		//return file.getData().length();
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 }