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.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
019import org.kuali.rice.krms.api.repository.proposition.PropositionParameter;
020import org.kuali.rice.krms.api.repository.proposition.PropositionParameterContract;
021import org.kuali.rice.krms.api.repository.term.TermDefinition;
022
023import javax.persistence.CascadeType;
024import javax.persistence.Column;
025import javax.persistence.Entity;
026import javax.persistence.FetchType;
027import javax.persistence.GeneratedValue;
028import javax.persistence.Id;
029import javax.persistence.JoinColumn;
030import javax.persistence.ManyToOne;
031import javax.persistence.Table;
032import javax.persistence.Transient;
033import javax.persistence.Version;
034import java.io.Serializable;
035import java.util.ArrayList;
036import java.util.Collections;
037import java.util.List;
038
039@Entity
040@Table(name = "KRMS_PROP_PARM_T")
041public class PropositionParameterBo implements PropositionParameterContract, Serializable {
042
043    private static final long serialVersionUID = 1l;
044
045    @PortableSequenceGenerator(name = "KRMS_PROP_PARM_S")
046    @GeneratedValue(generator = "KRMS_PROP_PARM_S")
047    @Id
048    @Column(name = "PROP_PARM_ID")
049    private String id;
050
051    @ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
052    @JoinColumn(name = "PROP_ID")
053    private PropositionBo proposition;
054
055    @Column(name = "PARM_VAL")
056    private String value;
057
058    @Column(name = "PARM_TYP_CD")
059    private String parameterType;
060
061    @Column(name = "SEQ_NO")
062    private Integer sequenceNumber;
063
064    @Transient
065    private TermDefinition termValue;
066
067    @Column(name = "VER_NBR")
068    private Long versionNumber = 0l;
069
070    public TermDefinition getTermValue() {
071        return termValue;
072    }
073
074    public void setTermValue(TermDefinition termValue) {
075        if (termValue != null) {
076            value = termValue.getId();
077        }
078
079        this.termValue = termValue;
080    }
081
082    /**
083     * Converts a mutable bo to it's immutable counterpart
084     *
085     * @param bo the mutable business object
086     * @return the immutable object
087     */
088    public static PropositionParameter to(PropositionParameterBo bo) {
089        if (bo == null) {
090            return null;
091        }
092
093        return PropositionParameter.Builder.create(bo).build();
094    }
095
096    /**
097     * Converts a list of mutable bos to it's immutable counterpart
098     *
099     * @param bos the list of smutable business objects
100     * @return and immutable list containing the immutable objects
101     */
102    public static List<PropositionParameter> to(List<PropositionParameterBo> bos) {
103        if (bos == null) {
104            return null;
105        }
106
107        List<PropositionParameter> parms = new ArrayList<PropositionParameter>();
108
109        for (PropositionParameterBo p : bos) {
110            parms.add(PropositionParameter.Builder.create(p).build());
111        }
112
113        return Collections.unmodifiableList(parms);
114    }
115
116    /**
117     * Converts a immutable object to it's mutable bo counterpart
118     *
119     * @param im immutable object
120     * @return the mutable bo
121     */
122    public static PropositionParameterBo from(PropositionParameter im) {
123        if (im == null) {
124            return null;
125        }
126
127        PropositionParameterBo bo = new PropositionParameterBo();
128        bo.id = im.getId();
129
130        // we don't set proposition here, it gets set in PropositionBo.from
131
132        bo.value = im.getValue();
133        bo.setTermValue(im.getTermValue());
134        bo.parameterType = im.getParameterType();
135        bo.sequenceNumber = im.getSequenceNumber();
136
137        if (im.getVersionNumber() == null) {
138            bo.setVersionNumber(0l);
139        } else {
140            bo.setVersionNumber(im.getVersionNumber());
141        }
142
143        return bo;
144    }
145
146    public static List<PropositionParameterBo> from(List<PropositionParameter> ims) {
147        if (ims == null) {
148            return null;
149        }
150
151        List<PropositionParameterBo> bos = new ArrayList<PropositionParameterBo>();
152        for (PropositionParameter im : ims) {
153            PropositionParameterBo bo = new PropositionParameterBo();
154            bo.id = im.getId();
155
156            // we don't set proposition here, it gets set in PropositionBo.from
157
158            bo.value = im.getValue();
159            bo.parameterType = im.getParameterType();
160            bo.sequenceNumber = im.getSequenceNumber();
161            bo.setVersionNumber(im.getVersionNumber());
162            bos.add(bo);
163        }
164
165        return Collections.unmodifiableList(bos);
166    }
167
168    public String getId() {
169        return id;
170    }
171
172    public void setId(String id) {
173        this.id = id;
174    }
175
176    public String getPropId() {
177        if (proposition != null) {
178            return proposition.getId();
179        }
180
181        return null;
182    }
183
184    public PropositionBo getProposition() {
185        return proposition;
186    }
187
188    public void setProposition(PropositionBo proposition) {
189        this.proposition = proposition;
190    }
191
192    public String getValue() {
193        return value;
194    }
195
196    public void setValue(String value) {
197        this.value = value;
198    }
199
200    public String getParameterType() {
201        return parameterType;
202    }
203
204    public void setParameterType(String parameterType) {
205        this.parameterType = parameterType;
206    }
207
208    public Integer getSequenceNumber() {
209        return sequenceNumber;
210    }
211
212    public void setSequenceNumber(Integer sequenceNumber) {
213        this.sequenceNumber = sequenceNumber;
214    }
215
216    public Long getVersionNumber() {
217        return versionNumber;
218    }
219
220    public void setVersionNumber(Long versionNumber) {
221        this.versionNumber = versionNumber;
222    }
223}