1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.model.common;
17
18 import java.util.LinkedHashMap;
19 import java.util.Map;
20
21 import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
22 import org.seasar.tuigwaa.database.DataRow;
23 import org.seasar.tuigwaa.database.DataTable;
24 import org.seasar.tuigwaa.model.DataService;
25 import org.seasar.tuigwaa.model.core.TgwEntity;
26 import org.seasar.tuigwaa.system.Constants;
27
28 /***
29 * Seasar のオブジェクトが参照できないクラスから
30 * データベースのデータを読み込みたいときに使うクラス
31 *
32 * @author nishioka
33 */
34 public class DataServiceUtils {
35
36 public static Object loadByValue(TgwEntity entity, String attrName, Object value){
37 return getDataService().loadByValue(entity, attrName, value);
38 }
39
40 public static DataTable getDataTable(TgwEntity entity, String filter){
41 return getDataService().find(entity, filter);
42 }
43
44 public static DataTable getDataTable(TgwEntity entity) {
45 return getDataService().list(entity);
46 }
47
48 private static DataService getDataService() {
49 return (DataService) SingletonS2ContainerFactory
50 .getContainer().getComponent(DataService.class);
51 }
52
53
54 /***
55 * 指定したモデル(テーブル)にある、各データの代表カラムの表示名と
56 * ID(主キー)の値を返す
57 */
58 public static Map getLabelValueMap(TgwEntity entity) {
59 Map map = new LinkedHashMap();
60 DataTable table = getDataTable(entity);
61 while (table.hasNext()) {
62 DataRow row = (DataRow) table.next();
63
64 String repre = entity.getRepresentativeField();
65 if (repre == null) {
66 repre = entity.getFirstField().getName();
67 }
68
69 String label = "" + row.getCell(repre);
70 Long id = (Long) row.getCell(Constants.ENTITY_BUILTIN_ID);
71 String value = String.valueOf(id);
72 map.put(label, value);
73 }
74 return map;
75 }
76 }