View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krms.impl.repository;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.mo.common.Versioned;
20  import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
21  import org.kuali.rice.krms.api.repository.action.ActionDefinition;
22  import org.kuali.rice.krms.api.repository.action.ActionDefinitionContract;
23  import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
24  
25  import javax.persistence.CascadeType;
26  import javax.persistence.Column;
27  import javax.persistence.Entity;
28  import javax.persistence.FetchType;
29  import javax.persistence.GeneratedValue;
30  import javax.persistence.Id;
31  import javax.persistence.JoinColumn;
32  import javax.persistence.ManyToOne;
33  import javax.persistence.OneToMany;
34  import javax.persistence.Table;
35  import javax.persistence.Version;
36  import java.io.Serializable;
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  /**
43   * The Action Business Object is the Action mutable class.
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   * @see ActionDefinition
47   * @see ActionDefinitionContract
48   * @see org.kuali.rice.krms.framework.engine.Action
49   */
50  @Entity
51  @Table(name = "KRMS_ACTN_T")
52  public class ActionBo implements ActionDefinitionContract, Versioned, Serializable {
53  
54      private static final long serialVersionUID = 1l;
55  
56      @PortableSequenceGenerator(name = "KRMS_ACTN_S")
57      @GeneratedValue(generator = "KRMS_ACTN_S")
58      @Id
59      @Column(name = "ACTN_ID")
60      private String id;
61  
62      @Column(name = "NMSPC_CD")
63      private String namespace;
64  
65      @Column(name = "NM")
66      private String name;
67  
68      @Column(name = "DESC_TXT")
69      private String description;
70  
71      @Column(name = "TYP_ID")
72      private String typeId;
73  
74      @ManyToOne()
75      @JoinColumn(name = "RULE_ID")
76      private RuleBo rule;
77  
78      @Column(name = "SEQ_NO")
79      private Integer sequenceNumber;
80  
81      @Column(name = "VER_NBR")
82      @Version
83      private Long versionNumber;
84  
85      @OneToMany(orphanRemoval = true, mappedBy = "action", fetch = FetchType.LAZY,
86              cascade = { CascadeType.REFRESH, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.PERSIST })
87      @JoinColumn(name = "ACTN_ATTR_DATA_ID", referencedColumnName = "ACTN_ATTR_DATA_ID")
88      private List<ActionAttributeBo> attributeBos;
89  
90      @Override
91      public Map<String, String> getAttributes() {
92          HashMap<String, String> attributes = new HashMap<String, String>();
93          if (attributeBos != null) for (ActionAttributeBo attr : attributeBos) {
94              if (attr.getAttributeDefinition() == null) {
95                  attributes.put("", "");
96              } else {
97                  attributes.put(attr.getAttributeDefinition().getName(), attr.getValue());
98              }
99          }
100         return attributes;
101     }
102 
103     /**
104      * Set the Action Attributes
105      *
106      * @param attributes to add to this Action
107      */
108     public void setAttributes(Map<String, String> attributes) {
109         this.attributeBos = new ArrayList<ActionAttributeBo>();
110         if (!StringUtils.isBlank(this.typeId)) {
111             List<KrmsAttributeDefinition> attributeDefinitions = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().findAttributeDefinitionsByType(this.getTypeId());
112             Map<String, KrmsAttributeDefinition> attributeDefinitionsByName = new HashMap<String, KrmsAttributeDefinition>();
113 
114             if (attributeDefinitions != null) {
115                 for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
116                     attributeDefinitionsByName.put(attributeDefinition.getName(), attributeDefinition);
117                 }
118             }
119 
120             for (Map.Entry<String, String> attr : attributes.entrySet()) {
121                 KrmsAttributeDefinition attributeDefinition = attributeDefinitionsByName.get(attr.getKey());
122 
123                 if (attributeDefinition != null) {
124                     ActionAttributeBo attributeBo = new ActionAttributeBo();
125                     attributeBo.setAction(this);
126                     attributeBo.setValue(attr.getValue());
127                     attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attributeDefinition));
128                     attributeBos.add(attributeBo);
129                 }
130             }
131         }
132     }
133 
134     /**
135      * Converts a mutable bo to it's immutable counterpart
136      *
137      * @param bo the mutable business object
138      * @return the immutable object
139      */
140     public static ActionDefinition to(ActionBo bo) {
141         if (bo == null) {
142             return null;
143         }
144 
145         return ActionDefinition.Builder.create(bo).build();
146     }
147 
148     /**
149      * Converts a immutable object to it's mutable bo counterpart
150      *
151      * @param im immutable object
152      * @return the mutable bo
153      */
154     public static ActionBo from(ActionDefinition im) {
155         if (im == null) {
156             return null;
157         }
158 
159         ActionBo bo = new ActionBo();
160         bo.id = im.getId();
161         bo.namespace = im.getNamespace();
162         bo.name = im.getName();
163         bo.typeId = im.getTypeId();
164         bo.description = im.getDescription();
165 
166         // we don't set the rule because we only have the ruleId in the ActionDefinition.  If you need the RuleBo as
167         // well, use RuleBo.from to convert the RuleDefinition and all it's children as well.
168 
169         bo.sequenceNumber = im.getSequenceNumber();
170         bo.setVersionNumber(im.getVersionNumber());
171 
172         // build the list of action attribute BOs
173         List<ActionAttributeBo> attrs = new ArrayList<ActionAttributeBo>();
174 
175         // for each converted pair, build an ActionAttributeBo and add it to the set
176         for (Map.Entry<String, String> entry : im.getAttributes().entrySet()) {
177             KrmsAttributeDefinitionBo attrDefBo = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().getKrmsAttributeBo(entry.getKey(), im.getNamespace());
178             ActionAttributeBo attributeBo = new ActionAttributeBo();
179             attributeBo.setAction(bo);
180             attributeBo.setValue(entry.getValue());
181             attributeBo.setAttributeDefinition(attrDefBo);
182             attrs.add(attributeBo);
183         }
184 
185         bo.setAttributeBos(attrs);
186 
187         return bo;
188     }
189 
190     public String getId() {
191         return id;
192     }
193 
194     public void setId(String id) {
195         this.id = id;
196     }
197 
198     public String getNamespace() {
199         return namespace;
200     }
201 
202     public void setNamespace(String namespace) {
203         this.namespace = namespace;
204     }
205 
206     public String getName() {
207         return name;
208     }
209 
210     public void setName(String name) {
211         this.name = name;
212     }
213 
214     public String getDescription() {
215         return description;
216     }
217 
218     public void setDescription(String description) {
219         this.description = description;
220     }
221 
222     public String getTypeId() {
223         return typeId;
224     }
225 
226     public void setTypeId(String typeId) {
227         this.typeId = typeId;
228     }
229 
230     public String getRuleId() {
231         if (rule != null) {
232             return rule.getId();
233         }
234 
235         return null;
236     }
237 
238     public RuleBo getRule() {
239         return rule;
240     }
241 
242     public void setRule(RuleBo rule) {
243         this.rule = rule;
244     }
245 
246     public Integer getSequenceNumber() {
247         return sequenceNumber;
248     }
249 
250     public void setSequenceNumber(Integer sequenceNumber) {
251         this.sequenceNumber = sequenceNumber;
252     }
253 
254     public Long getVersionNumber() {
255         return versionNumber;
256     }
257 
258     public void setVersionNumber(Long versionNumber) {
259         this.versionNumber = versionNumber;
260     }
261 
262     public List<ActionAttributeBo> getAttributeBos() {
263         return attributeBos;
264     }
265 
266     public void setAttributeBos(List<ActionAttributeBo> attributeBos) {
267         this.attributeBos = attributeBos;
268     }
269 }