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.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  }