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 com.isenshi.util.converter.function;
17  
18  import org.seasar.tuigwaa.model.common.EntityUtils;
19  import org.seasar.tuigwaa.model.core.TgwAttribute;
20  import org.seasar.tuigwaa.model.core.TgwEntity;
21  import org.seasar.tuigwaa.model.core.impl.FkAttribute;
22  import org.seasar.tuigwaa.model.core.impl.SelfAttribute;
23  
24  
25  public class Id2ObjConverterFunction extends ConverterFunction {
26  
27  	private TgwEntity getRefEntity() {
28  		TgwAttribute attr = getAttribute();
29  		if(attr instanceof FkAttribute){
30  			return ((FkAttribute)attr).getRefEntity();	
31  		}else if(attr instanceof SelfAttribute){
32  			return getEntity();
33  		}
34  		return null;
35  	}
36  	
37  	public Object evaluate(Object obj) {
38  		String src = getSourceField();
39  		String target = getTargetField();
40  
41  		Long id = (Long) EntityUtils.getProperty(obj, src);
42  		if (id != null && id.longValue() >= 0l) {
43  			Object targetObj = EntityUtils.newInstanceById(getRefEntity(), id);
44  			EntityUtils.setProperty(obj, target, targetObj);
45  		} else {
46  			EntityUtils.setProperty(obj, target, null);
47  		}
48  		return obj;
49  	}
50  }