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
17
18
19
20
21 public abstract class AExternalEntity {
22
23 protected AssociationManager associationManager = CoreFactory.getAssociationManager();
24
25
26 protected GenericValue genericValue;
27
28
29 protected final Map modifiedFields;
30
31
32 public AExternalEntity(GenericValue genericValue) {
33 this.genericValue = genericValue;
34 this.modifiedFields = new HashMap();
35 }
36
37
38
39
40
41
42
43 public void store() {
44 try {
45 if (!isCreated()) {
46
47 HashMap fields = new HashMap();
48
49 addKeys(fields);
50 addFields(fields);
51
52 genericValue = CoreFactory.getGenericDelegator().makeValue(getEntityName(), fields);
53 genericValue.create();
54
55 } else {
56 update();
57
58
59 }
60 init(genericValue);
61 } catch (GenericEntityException e) {
62 throw new DataAccessException("Error occurred while storing " + getEntityName(), e);
63 }
64 }
65
66
67
68
69 protected abstract void addFields(Map _fields);
70
71
72
73
74 protected void addKeys(Map _fields) {
75
76 }
77
78
79
80
81 public abstract String getEntityName();
82
83
84
85
86 public abstract Map getEntityKeys();
87
88
89
90
91 protected abstract void init(GenericValue _gv);
92
93
94
95
96
97 public abstract boolean equals(AExternalEntity _entity);
98
99
100
101
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
111
112
113 public void update() throws GenericEntityException {
114 load();
115 genericValue.setFields(getModifiedFields());
116 genericValue.store();
117 }
118
119
120
121
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
132
133 public Map getModifiedFields() {
134 return modifiedFields;
135 }
136
137
138
139
140 public GenericValue getGenericValue() {
141 return genericValue;
142 }
143
144
145
146
147 public boolean isCreated() {
148 return getGenericValue() != null;
149 }
150
151 }