001/** 002 * Copyright 2005-2016 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 java.io.Serializable; 019import java.util.ArrayList; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import javax.persistence.CascadeType; 025import javax.persistence.Column; 026import javax.persistence.Convert; 027import javax.persistence.Entity; 028import javax.persistence.FetchType; 029import javax.persistence.GeneratedValue; 030import javax.persistence.Id; 031import javax.persistence.JoinColumn; 032import javax.persistence.OneToMany; 033import javax.persistence.Table; 034import javax.persistence.Version; 035 036import org.apache.commons.lang.StringUtils; 037import org.kuali.rice.krad.data.CopyOption; 038import org.kuali.rice.krad.data.KradDataServiceLocator; 039import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator; 040import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter; 041import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition; 042import org.kuali.rice.krms.api.repository.context.ContextDefinition; 043import org.kuali.rice.krms.api.repository.context.ContextDefinitionContract; 044 045@Entity 046@Table(name = "KRMS_CNTXT_T") 047public class ContextBo implements ContextDefinitionContract, Serializable { 048 049 private static final long serialVersionUID = 1L; 050 051 public static final String CONTEXT_SEQ_NAME = "KRMS_CNTXT_S"; 052 053 @PortableSequenceGenerator(name = CONTEXT_SEQ_NAME) 054 @GeneratedValue(generator = CONTEXT_SEQ_NAME) 055 @Id 056 @Column(name = "CNTXT_ID") 057 private String id; 058 059 @Column(name = "NM") 060 private String name; 061 062 @Column(name = "NMSPC_CD") 063 private String namespace; 064 065 @Column(name = "TYP_ID") 066 private String typeId; 067 068 @Column(name = "DESC_TXT") 069 private String description; 070 071 @Column(name = "ACTV") 072 @Convert(converter = BooleanYNConverter.class) 073 private boolean active = true; 074 075 @OneToMany(mappedBy = "context") 076 @JoinColumn(name = "CNTXT_ID", referencedColumnName = "CNTXT_ID", insertable = false, updatable = false) 077 private List<AgendaBo> agendas = new ArrayList<AgendaBo>(); 078 079 @OneToMany( 080 targetEntity = ContextAttributeBo.class, orphanRemoval = true, mappedBy = "context", 081 cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST }, 082 fetch = FetchType.LAZY 083 ) 084 @JoinColumn(name = "CNTXT_ID", referencedColumnName = "CNTXT_ID", insertable = true, updatable = true) 085 private List<ContextAttributeBo> attributeBos = new ArrayList<ContextAttributeBo>(); 086 087 @Column(name = "VER_NBR") 088 @Version 089 private Long versionNumber; 090 091 @Override 092 public List<AgendaBo> getAgendas() { 093 return agendas; 094 } 095 096 @Override 097 public Map<String, String> getAttributes() { 098 Map<String, String> attributes = new HashMap<String, String>(); 099 100 if (attributeBos != null) { 101 for (ContextAttributeBo attr : attributeBos) { 102 ((HashMap<String, String>) attributes).put(attr.getAttributeDefinition().getName(), attr.getValue()); 103 } 104 } 105 106 return attributes; 107 } 108 109 public ContextBo copyContext(String additionalNameText) { 110 ContextBo copy = KradDataServiceLocator.getDataObjectService().copyInstance(this, CopyOption.RESET_PK_FIELDS, CopyOption.RESET_OBJECT_ID ); 111 //ContextBo copy = (ContextBo) SerializationUtils.deepCopy(this); 112 113 // 114 // set all IDs to null 115 // 116 117 copy.setId(null); 118 119 // copying a context does not copy the associated agendas 120 copy.setAgendas(null); 121 for (ContextAttributeBo attributeBo : copy.getAttributeBos()) { 122 attributeBo.setId(null); 123 attributeBo.setVersionNumber(null); 124 } 125 126 if (!StringUtils.isEmpty(additionalNameText)) { 127 copy.setName(copy.getName() + additionalNameText); 128 } 129 130 return copy; 131 } 132 133 /** 134 * Converts a mutable bo to it's immutable counterpart 135 * 136 * @param bo the mutable business object 137 * @return the immutable object 138 */ 139 public static ContextDefinition to(ContextBo bo) { 140 if (bo == null) { 141 return null; 142 } 143 144 return ContextDefinition.Builder.create(bo).build(); 145 } 146 147 /** 148 * Converts a immutable object to it's mutable bo counterpart 149 * 150 * @param im immutable object 151 * @return the mutable bo 152 */ 153 public static ContextBo from(ContextDefinition im) { 154 if (im == null) { 155 return null; 156 } 157 158 ContextBo bo = new ContextBo(); 159 bo.id = im.getId(); 160 bo.namespace = im.getNamespace(); 161 bo.name = im.getName(); 162 bo.typeId = im.getTypeId(); 163 bo.description = im.getDescription(); 164 bo.active = im.isActive(); 165 bo.agendas = new ArrayList<AgendaBo>(); 166 for (AgendaDefinition agenda : im.getAgendas()) { 167 bo.agendas.add(KrmsRepositoryServiceLocator.getAgendaBoService().from(agenda)); 168 } 169 170 // build the list of agenda attribute BOs 171 List<ContextAttributeBo> attrs = new ArrayList<ContextAttributeBo>(); 172 173 // for each converted pair, build an AgendaAttributeBo and add it to the list 174 ContextAttributeBo attributeBo; 175 for (Map.Entry<String, String> entry : im.getAttributes().entrySet()) { 176 KrmsAttributeDefinitionBo attrDefBo = 177 KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().getKrmsAttributeBo(entry.getKey(), im.getNamespace()); 178 attributeBo = new ContextAttributeBo(); 179 attributeBo.setContext(bo); 180 attributeBo.setValue(entry.getValue()); 181 attributeBo.setAttributeDefinition(attrDefBo); 182 attrs.add(attributeBo); 183 } 184 185 bo.setAttributeBos(attrs); 186 bo.versionNumber = im.getVersionNumber(); 187 188 return bo; 189 } 190 191 @Override 192 public String getId() { 193 return id; 194 } 195 196 public void setId(String id) { 197 this.id = id; 198 } 199 200 @Override 201 public String getName() { 202 return name; 203 } 204 205 public void setName(String name) { 206 this.name = name; 207 } 208 209 @Override 210 public String getNamespace() { 211 return namespace; 212 } 213 214 public void setNamespace(String namespace) { 215 this.namespace = namespace; 216 } 217 218 @Override 219 public String getTypeId() { 220 return typeId; 221 } 222 223 public void setTypeId(String typeId) { 224 this.typeId = typeId; 225 } 226 227 @Override 228 public String getDescription() { 229 return description; 230 } 231 232 public void setDescription(String description) { 233 this.description = description; 234 } 235 236 public boolean getActive() { 237 return active; 238 } 239 240 @Override 241 public boolean isActive() { 242 return active; 243 } 244 245 public void setActive(boolean active) { 246 this.active = active; 247 } 248 249 public void setAgendas(List<AgendaBo> agendas) { 250 this.agendas = agendas; 251 } 252 253 public List<ContextAttributeBo> getAttributeBos() { 254 return attributeBos; 255 } 256 257 public void setAttributeBos(List<ContextAttributeBo> attributeBos) { 258 this.attributeBos = attributeBos; 259 } 260 261 @Override 262 public Long getVersionNumber() { 263 return versionNumber; 264 } 265 266 public void setVersionNumber(Long versionNumber) { 267 this.versionNumber = versionNumber; 268 } 269}