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;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Comparator;
22  import java.util.List;
23  
24  import org.seasar.tuigwaa.model.common.EntityUtils;
25  import org.seasar.tuigwaa.model.common.TgwElementVisitor;
26  import org.seasar.tuigwaa.model.core.TgwAttribute;
27  import org.seasar.tuigwaa.model.core.TgwEntity;
28  import org.seasar.tuigwaa.view.wiki.DefaultDataViewVisitor;
29  import org.seasar.tuigwaa.view.wiki.ImageDataViewVisitor;
30  
31  import com.isenshi.util.functor.BinaryFunction;
32  
33  public class DataViewUtils {
34  
35  	public static class AttrComparator implements Comparator {
36  
37  		private String attrName;
38  
39  		private boolean desc;
40  		
41  		public AttrComparator(String attrName, boolean desc) {
42  			this.attrName = attrName;
43  			this.desc = desc;
44  		}
45  
46  		public int compare(Object o1, Object o2) {
47  			Object attrObj1 = EntityUtils.getProperty(o1, attrName);
48  			Object attrObj2 = EntityUtils.getProperty(o2, attrName);
49  
50  			if (attrObj1 == null || !(attrObj1 instanceof Comparable)
51  					|| attrObj2 == null || !(attrObj2 instanceof Comparable)) {
52  				return 0;
53  			}
54  
55  			Comparable c1 = (Comparable)attrObj1;
56  			Comparable c2 = (Comparable)attrObj2;
57  
58  			if(desc){
59  				return -c1.compareTo(c2);
60  			}else{
61  				return c1.compareTo(c2);
62  			}
63  		}
64  	}
65  
66  	public static final Comparator COMPARATOR_ID = new Comparator() {
67  		public int compare(Object o1, Object o2) {
68  			Long id1 = EntityUtils.getId(o1);
69  			Long id2 = EntityUtils.getId(o2);
70  			return id1.intValue() - id2.intValue();
71  		}
72  	};
73  
74  	public static List sortById(Collection collecion) {
75  		List list = new ArrayList();
76  		list.addAll(collecion);
77  		Collections.sort(list, COMPARATOR_ID);
78  		return list;
79  	}
80  
81  	public static List sortByAttr(Collection col , String attrName){
82  		return sortByAttr(col, attrName, false);
83  	}
84  	
85  	public static List sortByAttr(Collection col , String attrName, boolean desc){
86  		List list = new ArrayList();
87  		list.addAll(col);
88  		Collections.sort(list, new AttrComparator(attrName, desc));
89  		return list;
90  	}
91  		
92  	public static String getTableData(Object bean, TgwEntity entity , String attrName) {
93  		TgwAttribute attr = entity.getField(attrName);
94  		return getTableData(bean, attr);
95  	}
96  
97  	public static String getTableData(Object bean, TgwAttribute attr) {
98  		return getFilteredData(bean, attr, DefaultDataViewVisitor.INSTANCE);
99  	}
100 
101 	public static String getFilteredData(Object bean, TgwEntity entity, String attrName){
102 		TgwAttribute attr = entity.getField(attrName);
103 		return getFilteredData(bean, attr);
104 	}
105 	
106 	public static String getFilteredData(Object bean, TgwAttribute attr) {
107 		return getFilteredData(bean, attr, ImageDataViewVisitor.INSTANCE);
108 	}
109 
110 	private static String getFilteredData(Object bean, TgwAttribute attr,
111 			TgwElementVisitor visitor) {
112 
113 		Object data = EntityUtils.getProperty(bean, attr);
114 		if (data == null) {
115 			return "";
116 		}
117 		if (attr == null) {
118 			return "" + data;
119 		}
120 		BinaryFunction viewFunction = (BinaryFunction) attr.accept(visitor,
121 				null);
122 		if (viewFunction != null) {
123 			return "" + viewFunction.evaluate(data, bean);
124 		} else {
125 			return "" + data;
126 		}
127 	}
128 	
129 
130 
131 }