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 org.seasar.tuigwaa.util.functor;
17  
18  import java.util.Date;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.seasar.tuigwaa.model.common.EntityUtils;
23  import org.seasar.tuigwaa.util.TgwContext;
24  
25  import com.isenshi.util.CharUtil;
26  import com.isenshi.util.HtmlBuffer;
27  import com.isenshi.util.SimpleFile;
28  import com.isenshi.util.functor.BinaryFunction;
29  
30  public class FileDownloadFunction implements BinaryFunction {
31  
32  	private static final String DOWNLOAD_URL = "${context}/${site}/download.do?id=${id}&tableName=${tableName}&field=${field}&feed=${feed}";
33  
34  	private static final String FILE_URL = "/TGW-RES/img/file.gif";
35  
36  	private static final int DEFAULT_IMG_WIDTH = 200;
37  
38  	private String tableName;
39  
40  	private String fieldName;
41  
42  	private boolean linkOnly;
43  
44  	private String style;
45  
46  	public FileDownloadFunction(boolean linkOnly, String tableName,
47  			String fieldName) {
48  		this(linkOnly, tableName, fieldName, null);
49  	}
50  
51  	public FileDownloadFunction(boolean linkOnly, String tableName,
52  			String fieldName, String style) {
53  		this.linkOnly = linkOnly;
54  		this.fieldName = fieldName;
55  		this.tableName = tableName;
56  		this.style = style;
57  	}
58  
59  	private boolean isLinkOnly() {
60  		return linkOnly;
61  	}
62  
63  	private boolean isImage(String fileName) {
64  		return (fileName.toLowerCase().endsWith("jpg")
65  				|| fileName.toLowerCase().endsWith("bmp")
66  				|| fileName.toLowerCase().endsWith("gif") || fileName
67  				.toLowerCase().endsWith("png"));
68  	}
69  
70  	public Object evaluate(Object data, Object bean) {
71  		if (data == null) {
72  			return "";
73  		}
74  
75  		byte[] bytes = (byte[]) data;
76  		SimpleFile file = (SimpleFile) CharUtil.toObject(bytes);
77  		String fileName = file.getFileName();
78  		if (fileName == null || fileName.equals("")) {
79  			return "";
80  		}
81  
82  		Long id = EntityUtils.getId(bean);
83  		String href = getHref(id, tableName, fieldName);
84  
85  		if (isLinkOnly()) {
86  			return getAnchor(href, fileName);
87  		} else if (isImage(fileName)) {
88  			return getImgAnchor(href);
89  		} else {
90  			return getIconAnchor(href);
91  		}
92  	}
93  
94  	private String getAnchor(String href, String content) {
95  		HtmlBuffer buf = new HtmlBuffer();
96  		buf.appendStartTag("a");
97  		buf.appendAttribute("href", href);
98  		buf.appendBody(content);
99  		buf.endTag();
100 
101 		return buf.toString();
102 	}
103 
104 	private String getImgAnchor(String href) {
105 		HtmlBuffer buf = new HtmlBuffer();
106 		if (style != null) {
107 			buf.appendStartTag("div");
108 			buf.appendAttribute("style", style);
109 		}
110 		buf.appendStartTag("a");
111 		buf.appendAttribute("href", href);
112 		buf.appendAttribute("class", "imganchor");
113 		buf.appendStartTag("img");
114 		buf.appendAttribute("src", href);
115 		buf.appendAttribute("alt", href);
116 		//buf.appendAttribute("width", "" + DEFAULT_IMG_WIDTH);
117 		buf.endAllTags();
118 		return buf.toString();
119 	}
120 
121 	private String getIconAnchor(String href) {
122 		HtmlBuffer buf = new HtmlBuffer();
123 		if (style != null) {
124 			buf.appendStartTag("div");
125 			buf.appendAttribute("style", style);
126 		}
127 		buf.appendStartTag("a");
128 		buf.appendAttribute("href", href);
129 		buf.appendStartTag("img");
130 		buf.appendAttribute("src", TgwContext.getContextPath() + FILE_URL);
131 		buf.endAllTags();
132 		return buf.toString();
133 	}
134 
135 	public static String getHref(Long id, String tableName, String fieldName) {
136 		Map map = new HashMap();
137 		map.put("tableName", tableName);
138 		map.put("field", fieldName);
139 		map.put("id", String.valueOf(id));
140 		map.put("context", TgwContext.getContextPath());
141 		map.put("site", TgwContext.getSiteName());
142 		
143 		Date date = new Date();
144 		map.put("feed", "" + date.getTime());
145 		
146 		return CharUtil.replace(DOWNLOAD_URL, map);
147 	}
148 }