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