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.collections.CollectionUtils;
019import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
020import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
021import org.kuali.rice.krms.api.repository.category.CategoryDefinition;
022import org.kuali.rice.krms.api.repository.context.ContextDefinition;
023import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
024import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinitionContract;
025
026import javax.persistence.CascadeType;
027import javax.persistence.Column;
028import javax.persistence.Convert;
029import javax.persistence.Entity;
030import javax.persistence.FetchType;
031import javax.persistence.GeneratedValue;
032import javax.persistence.Id;
033import javax.persistence.JoinColumn;
034import javax.persistence.JoinTable;
035import javax.persistence.ManyToMany;
036import javax.persistence.OneToMany;
037import javax.persistence.Table;
038import javax.persistence.Transient;
039import javax.persistence.Version;
040import java.io.Serializable;
041import java.util.ArrayList;
042import java.util.List;
043
044@Entity
045@Table(name = "KRMS_TERM_SPEC_T")
046public class TermSpecificationBo implements TermSpecificationDefinitionContract, Serializable {
047
048    private static final long serialVersionUID = 1l;
049
050    @PortableSequenceGenerator(name = "KRMS_TERM_SPEC_S")
051    @GeneratedValue(generator = "KRMS_TERM_SPEC_S")
052    @Id
053    @Column(name = "TERM_SPEC_ID")
054    private String id;
055
056    @Column(name = "NM")
057    private String name;
058
059    @Column(name = "NMSPC_CD")
060    private String namespace;
061
062    @Column(name = "TYP")
063    private String type;
064
065    @Column(name = "DESC_TXT")
066    private String description;
067
068    @Column(name = "ACTV")
069    @Convert(converter = BooleanYNConverter.class)
070    private boolean active = true;
071
072    @Column(name = "VER_NBR")
073    @Version
074    private Long versionNumber;
075
076    @ManyToMany(targetEntity = CategoryBo.class, cascade = { CascadeType.REFRESH })
077    @JoinTable(name = "KRMS_TERM_SPEC_CTGRY_T",
078            joinColumns = { @JoinColumn(name = "TERM_SPEC_ID", referencedColumnName = "TERM_SPEC_ID") },
079            inverseJoinColumns = { @JoinColumn(name = "CTGRY_ID", referencedColumnName = "CTGRY_ID") })
080    private List<CategoryBo> categories = new ArrayList<CategoryBo>();
081
082    @OneToMany(orphanRemoval = true, mappedBy = "termSpecification", fetch = FetchType.LAZY,
083            cascade = { CascadeType.REFRESH, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.PERSIST })
084    @JoinColumn(name = "TERM_SPEC_ID", referencedColumnName = "TERM_SPEC_ID")
085    private List<ContextValidTermBo> contextValidTerms = new ArrayList<ContextValidTermBo>();
086
087    @Transient
088    private List<String> contextIds = new ArrayList<String>();
089
090    @Transient
091    private List<ContextBo> contexts = new ArrayList<ContextBo>();
092
093    /**
094     * Converts a mutable bo to it's immutable counterpart
095     *
096     * @param bo the mutable business object
097     * @return the immutable object
098     */
099    public static TermSpecificationDefinition to(TermSpecificationBo bo) {
100        if (bo == null) {
101            return null;
102        }
103
104        return TermSpecificationDefinition.Builder.create(bo).build();
105    }
106
107    /**
108     * Converts a immutable object to it's mutable bo counterpart
109     *
110     * @param im immutable object
111     * @return the mutable bo
112     */
113    public static TermSpecificationBo from(TermSpecificationDefinition im) {
114        if (im == null) {
115            return null;
116        }
117
118        TermSpecificationBo bo = new TermSpecificationBo();
119        bo.id = im.getId();
120        bo.namespace = im.getNamespace();
121        bo.name = im.getName();
122        bo.type = im.getType();
123        bo.description = im.getDescription();
124        bo.categories = new ArrayList<CategoryBo>();
125
126        for (CategoryDefinition category : im.getCategories()) {
127            bo.categories.add(CategoryBo.from(category));
128        }
129
130        bo.contextIds.addAll(im.getContextIds());
131        bo.active = im.isActive();
132        bo.setVersionNumber(im.getVersionNumber());
133
134        return bo;
135    }
136
137    public String getId() {
138        return id;
139    }
140
141    public void setId(String id) {
142        this.id = id;
143    }
144
145    public String getName() {
146        return name;
147    }
148
149    public void setName(String name) {
150        this.name = name;
151    }
152
153    public String getNamespace() {
154        return namespace;
155    }
156
157    public void setNamespace(String namespace) {
158        this.namespace = namespace;
159    }
160
161    public String getType() {
162        return type;
163    }
164
165    public void setType(String type) {
166        this.type = type;
167    }
168
169    public String getDescription() {
170        return description;
171    }
172
173    public void setDescription(String description) {
174        this.description = description;
175    }
176
177    public boolean getActive() {
178        return active;
179    }
180
181    public boolean isActive() {
182        return active;
183    }
184
185    public void setActive(boolean active) {
186        this.active = active;
187    }
188
189    public Long getVersionNumber() {
190        return versionNumber;
191    }
192
193    public void setVersionNumber(Long versionNumber) {
194        this.versionNumber = versionNumber;
195    }
196
197    public List<CategoryBo> getCategories() {
198        return categories;
199    }
200
201    public void setCategories(List<CategoryBo> categories) {
202        this.categories = categories;
203    }
204
205    public List<String> getContextIds() {
206        if(CollectionUtils.isEmpty(contextIds)){
207            for(ContextValidTermBo contextValidTermBo : this.getContextValidTerms()) {
208                contextIds.add(contextValidTermBo.getContextId());
209            }
210        }
211        return contextIds;
212    }
213
214    public void setContextIds(List<String> contextIds) {
215        this.contextIds = contextIds;
216    }
217
218    public List<ContextBo> getContexts() {
219        if(CollectionUtils.isEmpty(contexts)){
220            for(ContextValidTermBo contextValidTermBo : this.getContextValidTerms()) {
221                ContextDefinition context =
222                        KrmsRepositoryServiceLocator.getContextBoService().getContextByContextId(contextValidTermBo.getContextId());
223
224                if (context != null) {
225                    contexts.add(ContextBo.from(context));
226                }
227            }
228        }
229        return contexts;
230    }
231
232    public void setContexts(List<ContextBo> contexts) {
233        this.contexts = contexts;
234    }
235
236    public List<ContextValidTermBo> getContextValidTerms() {
237        return contextValidTerms;
238    }
239
240    public void setContextValidTerms(List<ContextValidTermBo> contextValidTerms) {
241        this.contextValidTerms = contextValidTerms;
242    }
243}