001/** 002 * Copyright 2005-2017 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krms.impl.repository; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.mo.common.Versioned; 020import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator; 021import org.kuali.rice.krms.api.repository.action.ActionDefinition; 022import org.kuali.rice.krms.api.repository.action.ActionDefinitionContract; 023import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition; 024 025import javax.persistence.CascadeType; 026import javax.persistence.Column; 027import javax.persistence.Entity; 028import javax.persistence.FetchType; 029import javax.persistence.GeneratedValue; 030import javax.persistence.Id; 031import javax.persistence.JoinColumn; 032import javax.persistence.ManyToOne; 033import javax.persistence.OneToMany; 034import javax.persistence.Table; 035import javax.persistence.Version; 036import java.io.Serializable; 037import java.util.ArrayList; 038import java.util.HashMap; 039import java.util.List; 040import java.util.Map; 041 042/** 043 * The Action Business Object is the Action mutable class. 044 * 045 * @author Kuali Rice Team (rice.collab@kuali.org) 046 * @see ActionDefinition 047 * @see ActionDefinitionContract 048 * @see org.kuali.rice.krms.framework.engine.Action 049 */ 050@Entity 051@Table(name = "KRMS_ACTN_T") 052public class ActionBo implements ActionDefinitionContract, Versioned, Serializable { 053 054 private static final long serialVersionUID = 1l; 055 056 @PortableSequenceGenerator(name = "KRMS_ACTN_S") 057 @GeneratedValue(generator = "KRMS_ACTN_S") 058 @Id 059 @Column(name = "ACTN_ID") 060 private String id; 061 062 @Column(name = "NMSPC_CD") 063 private String namespace; 064 065 @Column(name = "NM") 066 private String name; 067 068 @Column(name = "DESC_TXT") 069 private String description; 070 071 @Column(name = "TYP_ID") 072 private String typeId; 073 074 @ManyToOne() 075 @JoinColumn(name = "RULE_ID") 076 private RuleBo rule; 077 078 @Column(name = "SEQ_NO") 079 private Integer sequenceNumber; 080 081 @Column(name = "VER_NBR") 082 @Version 083 private Long versionNumber; 084 085 @OneToMany(orphanRemoval = true, mappedBy = "action", fetch = FetchType.LAZY, 086 cascade = { CascadeType.REFRESH, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.PERSIST }) 087 @JoinColumn(name = "ACTN_ATTR_DATA_ID", referencedColumnName = "ACTN_ATTR_DATA_ID") 088 private List<ActionAttributeBo> attributeBos; 089 090 @Override 091 public Map<String, String> getAttributes() { 092 HashMap<String, String> attributes = new HashMap<String, String>(); 093 if (attributeBos != null) for (ActionAttributeBo attr : attributeBos) { 094 if (attr.getAttributeDefinition() == null) { 095 attributes.put("", ""); 096 } else { 097 attributes.put(attr.getAttributeDefinition().getName(), attr.getValue()); 098 } 099 } 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}