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 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 /** 110 * Converts a mutable bo to it's immutable counterpart 111 * 112 * @param bo the mutable business object 113 * @return the immutable object 114 */ 115 public static ContextDefinition to(ContextBo bo) { 116 if (bo == null) { 117 return null; 118 } 119 120 return ContextDefinition.Builder.create(bo).build(); 121 } 122 123 /** 124 * Converts a immutable object to it's mutable bo counterpart 125 * 126 * @param im immutable object 127 * @return the mutable bo 128 */ 129 public static ContextBo from(ContextDefinition im) { 130 if (im == null) { 131 return null; 132 } 133 134 ContextBo bo = new ContextBo(); 135 bo.id = im.getId(); 136 bo.namespace = im.getNamespace(); 137 bo.name = im.getName(); 138 bo.typeId = im.getTypeId(); 139 bo.description = im.getDescription(); 140 bo.active = im.isActive(); 141 bo.agendas = new ArrayList<AgendaBo>(); 142 for (AgendaDefinition agenda : im.getAgendas()) { 143 bo.agendas.add(KrmsRepositoryServiceLocator.getAgendaBoService().from(agenda)); 144 } 145 146 // build the list of agenda attribute BOs 147 List<ContextAttributeBo> attrs = new ArrayList<ContextAttributeBo>(); 148 149 // for each converted pair, build an AgendaAttributeBo and add it to the list 150 ContextAttributeBo attributeBo; 151 for (Map.Entry<String, String> entry : im.getAttributes().entrySet()) { 152 KrmsAttributeDefinitionBo attrDefBo = 153 KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().getKrmsAttributeBo(entry.getKey(), im.getNamespace()); 154 attributeBo = new ContextAttributeBo(); 155 attributeBo.setContext(bo); 156 attributeBo.setValue(entry.getValue()); 157 attributeBo.setAttributeDefinition(attrDefBo); 158 attrs.add(attributeBo); 159 } 160 161 bo.setAttributeBos(attrs); 162 bo.versionNumber = im.getVersionNumber(); 163 164 return bo; 165 } 166 167 @Override 168 public String getId() { 169 return id; 170 } 171 172 public void setId(String id) { 173 this.id = id; 174 } 175 176 @Override 177 public String getName() { 178 return name; 179 } 180 181 public void setName(String name) { 182 this.name = name; 183 } 184 185 @Override 186 public String getNamespace() { 187 return namespace; 188 } 189 190 public void setNamespace(String namespace) { 191 this.namespace = namespace; 192 } 193 194 @Override 195 public String getTypeId() { 196 return typeId; 197 } 198 199 public void setTypeId(String typeId) { 200 this.typeId = typeId; 201 } 202 203 @Override 204 public String getDescription() { 205 return description; 206 } 207 208 public void setDescription(String description) { 209 this.description = description; 210 } 211 212 public boolean getActive() { 213 return active; 214 } 215 216 @Override 217 public boolean isActive() { 218 return active; 219 } 220 221 public void setActive(boolean active) { 222 this.active = active; 223 } 224 225 public void setAgendas(List<AgendaBo> agendas) { 226 this.agendas = agendas; 227 } 228 229 public List<ContextAttributeBo> getAttributeBos() { 230 return attributeBos; 231 } 232 233 public void setAttributeBos(List<ContextAttributeBo> attributeBos) { 234 this.attributeBos = attributeBos; 235 } 236 237 @Override 238 public Long getVersionNumber() { 239 return versionNumber; 240 } 241 242 public void setVersionNumber(Long versionNumber) { 243 this.versionNumber = versionNumber; 244 } 245}