001/**
002 * Copyright 2005-2014 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.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
019import org.kuali.rice.krms.api.repository.term.TermDefinition;
020import org.kuali.rice.krms.api.repository.term.TermDefinitionContract;
021import org.kuali.rice.krms.api.repository.term.TermParameterDefinition;
022
023import javax.persistence.CascadeType;
024import javax.persistence.Column;
025import javax.persistence.Entity;
026import javax.persistence.GeneratedValue;
027import javax.persistence.Id;
028import javax.persistence.JoinColumn;
029import javax.persistence.ManyToOne;
030import javax.persistence.OneToMany;
031import javax.persistence.Table;
032import javax.persistence.Transient;
033import javax.persistence.Version;
034import java.io.Serializable;
035import java.util.ArrayList;
036import java.util.HashMap;
037import java.util.List;
038import java.util.Map;
039import java.util.Map.Entry;
040
041/**
042 * This is an override to set cascade merge on "parameters" member. See KULRICE-12614
043 */
044@Entity
045@Table(name = "KRMS_TERM_T")
046public class TermBo implements TermDefinitionContract, Serializable {
047
048    private static final long serialVersionUID = 1l;
049
050    public static final String TERM_SEQ_NAME = "KRMS_TERM_S";
051
052    @PortableSequenceGenerator(name = TERM_SEQ_NAME) @GeneratedValue(generator = TERM_SEQ_NAME) @Id @Column(
053            name = "TERM_ID")
054    private String id;
055
056    @Column(name = "TERM_SPEC_ID")
057    private String specificationId;
058
059    @Column(name = "DESC_TXT")
060    private String description;
061
062    @Version @Column(name = "VER_NBR", length = 8)
063    protected Long versionNumber;
064
065    // new-ing up an empty one allows the TermBo lookup to work on fields in the term specification:
066    @ManyToOne(targetEntity = TermSpecificationBo.class, cascade = {CascadeType.REFRESH})
067    @JoinColumn(name = "TERM_SPEC_ID", referencedColumnName = "TERM_SPEC_ID", insertable = false, updatable = false)
068    private TermSpecificationBo specification = new TermSpecificationBo();
069
070    @OneToMany(targetEntity = TermParameterBo.class, orphanRemoval = true,
071            cascade = {CascadeType.REFRESH, CascadeType.MERGE, CascadeType.REMOVE, CascadeType.PERSIST},
072            mappedBy = "term"
073    )
074    private List<TermParameterBo> parameters;
075
076    @Transient
077    private Map<String, String> parametersMap = new HashMap<String, String>();
078
079    /**
080     * Converts a mutable bo to it's immutable counterpart
081     *
082     * @param bo the mutable business object
083     * @return the immutable object
084     */
085    public static TermDefinition to(TermBo bo) {
086        if (bo == null) {
087            return null;
088        }
089
090        return org.kuali.rice.krms.api.repository.term.TermDefinition.Builder.create(bo).build();
091    }
092
093    /**
094     * Converts a immutable object to it's mutable bo counterpart
095     *
096     * @param im immutable object
097     * @return the mutable bo
098     */
099    public static TermBo from(TermDefinition im) {
100        if (im == null) {
101            return null;
102        }
103
104        TermBo bo = new TermBo();
105        bo.id = im.getId();
106        bo.specificationId = im.getSpecification().getId();
107        bo.description = im.getDescription();
108        bo.specification = TermSpecificationBo.from(im.getSpecification());
109        bo.parameters = new ArrayList<TermParameterBo>();
110
111        for (TermParameterDefinition parm : im.getParameters()) {
112            TermParameterBo termParmBo = TermParameterBo.from(parm);
113            bo.parameters.add(termParmBo);
114            termParmBo.setTerm(bo);
115        }
116
117        bo.versionNumber = im.getVersionNumber();
118
119        return bo;
120    }
121
122    public TermSpecificationBo getSpecification() {
123        return specification;
124    }
125
126    public void setSpecification(TermSpecificationBo specification) {
127        this.specification = specification;
128    }
129
130    public List<TermParameterBo> getParameters() {
131        return parameters;
132    }
133
134    public Map<String, String> getParametersMap() {
135        return parametersMap;
136    }
137
138    public void setParameters(List<TermParameterBo> parameters) {
139        this.parameters = parameters;
140    }
141
142    public void exportToParametersMap() {
143        // merge in TermParameterBo values
144        if (parameters != null) {
145            for (TermParameterBo param : parameters) {
146                parametersMap.put(param.getName(), param.getValue());
147            }
148        }
149    }
150
151    public void importFromParametersMap() {
152        if (parameters == null) {
153            parameters = new ArrayList<TermParameterBo>();
154        } else {
155            parameters.clear();
156        }
157
158        for (Entry<String, String> paramEntry : parametersMap.entrySet()) {
159            TermParameterDefinition termDef = TermParameterDefinition.Builder.create(null, id, paramEntry.getKey(),
160                    paramEntry.getValue()).build();
161            parameters.add(TermParameterBo.from(termDef));
162        }
163    }
164
165    public String getDescription() {
166        return description;
167    }
168
169    public void setDescription(String description) {
170        this.description = description;
171    }
172
173    public String getSpecificationId() {
174        return specificationId;
175    }
176
177    public void setSpecificationId(String specificationId) {
178        this.specificationId = specificationId;
179    }
180
181    public String getId() {
182        return id;
183    }
184
185    public void setId(String id) {
186        this.id = id;
187    }
188
189    public Long getVersionNumber() {
190        return versionNumber;
191    }
192
193    public void setVersionNumber(Long versionNumber) {
194        this.versionNumber = versionNumber;
195    }
196}