View Javadoc

1   package com.atlassian.jira.referentiel.entities;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.ofbiz.core.entity.EntityUtil;
7   import org.ofbiz.core.entity.GenericDelegator;
8   import org.ofbiz.core.entity.GenericEntityException;
9   import org.ofbiz.core.entity.GenericValue;
10  
11  import com.atlassian.core.ofbiz.CoreFactory;
12  import com.atlassian.core.ofbiz.association.AssociationManager;
13  import com.atlassian.jira.exception.DataAccessException;
14  
15  /**
16   * FQCN : com.atlassian.jira.referentiel.entities.AExternalEntity
17   * @author Kaamelot
18   * @since
19   * Description : AExternalEntity provides default behavior for any Entity appended to the JIRA model 
20   */
21  public abstract class AExternalEntity {
22  
23  	protected AssociationManager associationManager = CoreFactory.getAssociationManager();
24  
25  	// -- Persistants Fields ---------
26  	protected GenericValue genericValue;
27  
28  	// --
29  	protected final Map modifiedFields;
30  	
31  	// -- Constructors ---------
32  	public AExternalEntity(GenericValue genericValue) {
33  		this.genericValue = genericValue;
34  		this.modifiedFields = new HashMap();
35  	}	
36  
37  	// -- Abstracts Methods ---------
38  	/**
39  	 * Performs the Entity storage
40  	 */
41  //	public abstract void store();
42  	
43  	public void store() {
44  		try {
45  			if (!isCreated()) {
46  				// If we do not have a generic value we need to create a new record
47  				HashMap fields = new HashMap();
48  				
49  				addKeys(fields);
50  				addFields(fields);
51  
52  				genericValue = CoreFactory.getGenericDelegator().makeValue(getEntityName(), fields);
53  				genericValue.create();
54  //				init(genericValue);
55  			} else {
56  				update();
57  				// Otherwise, just update the record of the issue
58  //				genericValue.store();
59  			}
60  			init(genericValue);
61  		} catch (GenericEntityException e) {
62  			throw new DataAccessException("Error occurred while storing " + getEntityName(), e);
63  		}
64  	}
65  	
66  	/** Add Field to MAP for OFBIZ Storage
67  	 * @param _fields field Maps
68  	 */
69  	protected abstract void addFields(Map _fields);	
70  
71  	/** Add Key Fields to MAP for OFBIZ Storage
72  	 * @param _fields field Maps
73  	 */
74  	protected void addKeys(Map _fields) {
75  		// Nothing to do by default for ExternalEntities  
76  	}
77  	
78  	/**
79  	 * @return OFBIZ Entity Name 
80  	 */
81  	public abstract String getEntityName();
82  
83  	/**
84  	 * @return Map of field to assume as Key of the current Entity 
85  	 */
86  	public abstract Map getEntityKeys();
87  	
88  	/** Initialize final Object (Attributes population) 
89  	 * @param _gv linked GenericValue 
90  	 */
91  	protected abstract void init(GenericValue _gv);
92  	
93  	/**
94  	 * @param _entity
95  	 * @return True if _entity assumed as equal to current entity
96  	 */
97  	public abstract boolean equals(AExternalEntity _entity);
98  
99  	/**
100 	 * Retrieves GenericValue for the current entity depending on the fields identified as keys 
101 	 * @throws GenericEntityException
102 	 */
103 	public void load() throws GenericEntityException {
104 		GenericDelegator gd = CoreFactory.getGenericDelegator();
105 		GenericValue gv = EntityUtil.getOnly(gd.findByAnd(getEntityName(), getEntityKeys()));
106 		genericValue = gv;		
107 	}
108 	
109 	/**
110 	 * Performs the update (storage) using the modified Field
111 	 * @throws GenericEntityException
112 	 */	
113 	public void update() throws GenericEntityException {
114 		load();
115 		genericValue.setFields(getModifiedFields());
116 		genericValue.store();
117 	}
118 
119 	/** Update field Object and mark it as modified and requiring a storage
120 	 * @param _fieldName 
121 	 * @param _fieldValue
122 	 */
123 	protected void updateGV(final String _fieldName, final Object _fieldValue) {
124 		if (genericValue != null) {
125 			genericValue.set(_fieldName, _fieldValue);
126 		}
127 		modifiedFields.put(_fieldName, _fieldValue);
128 	}
129 
130 	/**
131 	 * @return Map of all fields marked as modified 
132 	 */
133 	public Map getModifiedFields() {
134 		return modifiedFields;
135 	}
136 
137 	/**
138 	 * @return The linked GenericValue
139 	 */
140 	public GenericValue getGenericValue() {
141 		return genericValue;
142 	}
143 
144 	/**
145 	 * @return True, the GenericValue has already been created/stored
146 	 */
147 	public boolean isCreated() {
148 		return getGenericValue() != null;
149 	}
150 	
151 }