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.view.wiki;
17  
18  import java.text.SimpleDateFormat;
19  
20  import org.seasar.tuigwaa.model.common.EntityUtils;
21  import org.seasar.tuigwaa.model.common.TgwElementVisitor;
22  import org.seasar.tuigwaa.model.core.TgwAttribute;
23  import org.seasar.tuigwaa.model.core.TgwDomain;
24  import org.seasar.tuigwaa.model.core.TgwEntity;
25  import org.seasar.tuigwaa.model.core.impl.BooleanAttribute;
26  import org.seasar.tuigwaa.model.core.impl.DateAttribute;
27  import org.seasar.tuigwaa.model.core.impl.DoubleAttribute;
28  import org.seasar.tuigwaa.model.core.impl.FileAttribute;
29  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
30  import org.seasar.tuigwaa.model.core.impl.FloatAttribute;
31  import org.seasar.tuigwaa.model.core.impl.IntegerAttribute;
32  import org.seasar.tuigwaa.model.core.impl.LongAttribute;
33  import org.seasar.tuigwaa.model.core.impl.SecurityAttribute;
34  import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
35  import org.seasar.tuigwaa.model.core.impl.SetAttribute;
36  import org.seasar.tuigwaa.model.core.impl.StringAttribute;
37  import org.seasar.tuigwaa.model.core.impl.TimestampAttribute;
38  import org.seasar.tuigwaa.util.TgwResource;
39  import org.seasar.tuigwaa.util.functor.ArrayViewerFunction;
40  import org.seasar.tuigwaa.util.functor.BinaryUnaryFunction;
41  import org.seasar.tuigwaa.util.functor.DateFilterFunction;
42  import org.seasar.tuigwaa.util.functor.FileDownloadFunction;
43  import org.seasar.tuigwaa.util.functor.Op;
44  import org.seasar.tuigwaa.util.functor.PropertyFunction;
45  import org.seasar.tuigwaa.util.functor.ReplaceStringFunction;
46  import org.seasar.tuigwaa.util.functor.StringBooleanFunction;
47  import org.seasar.tuigwaa.util.functor.SubstringFunction;
48  import org.seasar.tuigwaa.util.functor.TextEscapeFunction;
49  
50  import com.isenshi.util.functor.BinaryFunction;
51  import com.isenshi.util.functor.CompositeBinaryFunction;
52  import com.isenshi.util.functor.CompositeUnaryFunction;
53  import com.isenshi.util.functor.UnaryFunction;
54  
55  public class DefaultDataViewVisitor implements TgwElementVisitor {
56  
57  	public static final DefaultDataViewVisitor INSTANCE = new DefaultDataViewVisitor();
58  
59  	protected static final String EMAIL_TEMPLATE = "<a href='mailto:${0}'>${0}</a>";
60  
61  	protected static final String URL_TEMPLATE = "<a href='${0}'>${0}</a>";
62  
63  	private UnaryFunction substringFunction = new SubstringFunction(10);
64  
65  	private UnaryFunction escapeFunction = new TextEscapeFunction();
66  
67  	protected DefaultDataViewVisitor() {
68  	}
69  
70  	public Object visit(DoubleAttribute field, Object data) {
71  		return null;
72  	}
73  
74  	public Object visit(LongAttribute field, Object data) {
75  		return null;
76  	}
77  
78  	public Object visit(SelfAttribute field, Object data) {
79  		return (BinaryFunction) field.getEntity().accept(this, data);
80  	}
81  
82  	public Object visit(IntegerAttribute field, Object data) {
83  		return null;
84  	}
85  
86  	public Object visit(FloatAttribute field, Object data) {
87  		return null;
88  	}
89  
90  	public Object visit(BooleanAttribute field, Object data) {
91  		String trueStr = field.getTrueStr();
92  		String falseStr = field.getFalseStr();
93  		if (trueStr == null || falseStr == null) {
94  			return null;
95  		}
96  		return toBinary(new StringBooleanFunction(trueStr, falseStr));
97  	}
98  
99  	public Object visit(StringAttribute field, Object data) {
100 		String mask = field.getMask();
101 		if (StringAttribute.MASK_EMAIL.equals(mask)) {
102 			return toBinary(new ReplaceStringFunction(EMAIL_TEMPLATE));
103 		} else if (StringAttribute.MASK_URL.equals(mask)) {
104 			return toBinary(new ReplaceStringFunction(URL_TEMPLATE));
105 		} else if (StringAttribute.MASK_CONTEXT.equals(mask)
106 				|| StringAttribute.MASK_WIKI.equals(mask)
107 				|| StringAttribute.MASK_SHORTCONTEXT.equals(mask)) {
108 			return toBinary(escape(substringFunction));
109 		} else if (StringAttribute.MASK_NORMAL.equals(mask)) {
110 			return toBinary(escapeFunction);
111 		}
112 		return null;
113 	}
114 
115 	public Object visit(DateAttribute field, Object data) {
116 		String dateformat = TgwResource.getProperty("option.display.date");
117 		SimpleDateFormat format = new SimpleDateFormat(dateformat);
118 		return toBinary(new DateFilterFunction(format));
119 	}
120 
121 	public Object visit(FileAttribute field, Object data) {
122 		String tableName = field.getEntity().getName();
123 		String fieldName = field.getName();
124 		return new FileDownloadFunction(true, tableName, fieldName);
125 	}
126 
127 	public Object visit(SecurityAttribute field, Object data) {
128 		return null;
129 	}
130 
131 	public Object visit(TimestampAttribute field, Object data) {
132 		String dateformat = TgwResource.getProperty("option.display.timestamp");
133 		SimpleDateFormat format = new SimpleDateFormat(dateformat);
134 		return toBinary(new DateFilterFunction(format));
135 	}
136 
137 	public Object visit(SetAttribute field, Object data) {
138 		if (EntityUtils.manyToManyFlag(field)) {
139 			TgwAttribute theOtherAttr = EntityUtils.getTheOtherAttribute(field);
140 			BinaryFunction function = new CompositeBinaryFunction(
141 					(BinaryFunction) theOtherAttr.accept(this, data),
142 					new BinaryUnaryFunction(new PropertyFunction(theOtherAttr
143 							.getName())));
144 			return toBinary(new ArrayViewerFunction(function));
145 		} else {
146 			return toBinary(Op.COLLECTIONSIZE_FUNCTION);
147 		}
148 	}
149 
150 	public Object visit(FkAttribute field, Object data) {
151 		// String attrName = field.getName();
152 		TgwEntity refEntity = field.getRefEntity();
153 		return (BinaryFunction) refEntity.accept(this, data);
154 		// BinaryFunction propFunction = new BinaryUnaryFunction(new
155 		// PropertyFunction(attrName));
156 		// BinaryFunction binaryFunction =
157 		// return new CompositeBinaryFunction(binaryFunction, propFunction);
158 		// String nestedField = field.getRefEntity()
159 		// .getRecursiveRepresentativeField();
160 		// return new BinaryUnaryFunction(new PropertyFunction(nestedField));
161 	}
162 
163 	public Object visit(TgwEntity entity, Object data) {
164 		String fieldName = entity.getRepresentativeField();
165 		TgwAttribute field = entity.getField(fieldName);
166 		BinaryFunction propertyFunction = new BinaryUnaryFunction(
167 				new PropertyFunction(fieldName));
168 		BinaryFunction fieldViewFunction = (BinaryFunction) field.accept(this,
169 				data);
170 		if (fieldViewFunction == null) {
171 			fieldViewFunction = Op.NOTHING_BINARY_FUNCTION;
172 		}
173 		return new CompositeBinaryFunction(fieldViewFunction, propertyFunction);
174 	}
175 
176 	public Object visit(TgwDomain domain, Object data) {
177 		return null;
178 	}
179 
180 	protected Object toBinary(UnaryFunction unary) {
181 		return new BinaryUnaryFunction(unary);
182 	}
183 
184 	protected UnaryFunction escape(UnaryFunction unary) {
185 		return new CompositeUnaryFunction(unary, escapeFunction);
186 	}
187 }