View Javadoc

1   package com.atlassian.jira.referentiel.entities;
2   
3   import java.util.Map;
4   
5   import org.ofbiz.core.entity.EntityUtil;
6   import org.ofbiz.core.entity.GenericDelegator;
7   import org.ofbiz.core.entity.GenericEntityException;
8   import org.ofbiz.core.entity.GenericValue;
9   import org.ofbiz.core.util.UtilMisc;
10  
11  import com.atlassian.core.ofbiz.CoreFactory;
12  import com.atlassian.core.ofbiz.util.EntityUtils;
13  
14  public abstract class AInternalEntity extends AExternalEntity {
15  
16  	public AInternalEntity(GenericValue genericValue) {
17  		super(genericValue);
18  		init(genericValue);
19  	}
20  
21  	public static final String ID = "id";
22  
23  	private Long id = null;
24  	
25  	public Map getEntityKeys() {
26  		return UtilMisc.toMap(ID,id);
27  	}
28  	
29  	/**
30  	 * @return Map of field to assume as Key of the current Entity 
31  	 */
32  	public abstract Map getEntityCandidateKeys();
33  	
34  	/** Add Key Fields to MAP for OFBIZ Storage
35  	 * @param _fields field Maps
36  	 */
37  	protected void addKeys(Map _fields) {
38  		try {
39  			if (id==null) {
40  				id = new Long(EntityUtils.getNextStringId(getEntityName()));
41  			}
42  			_fields.putAll(getEntityKeys());
43  		} catch (Exception e) {
44  		}
45  	}
46  	
47  	/**
48  	 * Retrieves GenericValue for the current entity depending on the fields identified as keys 
49  	 * @throws GenericEntityException
50  	 */
51  	public void load() throws GenericEntityException {
52  		super.load();
53  		if (genericValue == null) {
54  			GenericDelegator gd = CoreFactory.getGenericDelegator();
55  			genericValue = EntityUtil.getOnly(gd.findByAnd(getEntityName(), getEntityCandidateKeys()));
56  		} 
57  	}
58  	
59  	/**
60  	 * @return True, the GenericValue has already been created/stored
61  	 */
62  	public boolean isCreated() {
63  		boolean isCreated = false;
64  		try {
65  			load();
66  			isCreated = (getGenericValue() != null);
67  //			if (!isCreated) {
68  //				id = new Long(EntityUtils.getNextStringId(getEntityName()));
69  //			}
70  		} catch (Exception e) {
71  		}
72  //		if (!isCreated) {
73  //			try {
74  //				id = new Long(EntityUtils.getNextStringId(getEntityName()));
75  //			} catch (Exception e) {
76  //				throw new DataAccessException("Error occurred while storing " + getEntityName(), e);
77  //			}
78  //		}
79  		return isCreated;
80  	}
81  
82  	public Long getId() { return id; }
83  
84  	public void setId(Long i) { id = i; }
85  	
86  }