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.core.api.mo.common.Versioned;
019import org.kuali.rice.krad.data.jpa.converters.BooleanYNConverter;
020import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
021import org.kuali.rice.krms.api.repository.term.TermResolverDefinition;
022import org.kuali.rice.krms.api.repository.term.TermResolverDefinitionContract;
023import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
024
025import javax.persistence.CascadeType;
026import javax.persistence.Column;
027import javax.persistence.Convert;
028import javax.persistence.Entity;
029import javax.persistence.FetchType;
030import javax.persistence.GeneratedValue;
031import javax.persistence.Id;
032import javax.persistence.JoinColumn;
033import javax.persistence.JoinTable;
034import javax.persistence.ManyToMany;
035import javax.persistence.ManyToOne;
036import javax.persistence.OneToMany;
037import javax.persistence.Table;
038import javax.persistence.Transient;
039import javax.persistence.Version;
040import java.io.Serializable;
041import java.util.Collections;
042import java.util.HashMap;
043import java.util.HashSet;
044import java.util.Map;
045import java.util.Set;
046
047@Entity
048@Table(name = "KRMS_TERM_RSLVR_T")
049public class TermResolverBo implements TermResolverDefinitionContract, Versioned, Serializable {
050
051    private static final long serialVersionUID = 1l;
052
053    @PortableSequenceGenerator(name = "KRMS_TERM_RSLVR_S")
054    @GeneratedValue(generator = "KRMS_TERM_RSLVR_S")
055    @Id
056    @Column(name = "TERM_RSLVR_ID")
057    private String id;
058
059    @Column(name = "NMSPC_CD")
060    private String namespace;
061
062    @Column(name = "NM")
063    private String name;
064
065    @Transient
066    private String contextId;
067
068    @Column(name = "TYP_ID")
069    private String typeId;
070
071    @Column(name = "OUTPUT_TERM_SPEC_ID")
072    private String outputId;
073
074    @Column(name = "ACTV")
075    @Convert(converter = BooleanYNConverter.class)
076    private boolean active = true;
077
078    @Version
079    @Column(name="VER_NBR", length=8)
080    protected Long versionNumber;
081
082    @ManyToOne(targetEntity = TermSpecificationBo.class, cascade = { CascadeType.REFRESH })
083    @JoinColumn(name = "OUTPUT_TERM_SPEC_ID", insertable = false, updatable = false)
084    private TermSpecificationBo output;
085
086    @ManyToMany(targetEntity = TermSpecificationBo.class, cascade = { CascadeType.REFRESH })
087    @JoinTable(name = "KRMS_TERM_RSLVR_INPUT_SPEC_T",
088            joinColumns = { @JoinColumn(name = "TERM_RSLVR_ID", referencedColumnName = "TERM_RSLVR_ID") },
089            inverseJoinColumns = { @JoinColumn(name = "TERM_SPEC_ID", referencedColumnName = "TERM_SPEC_ID") }
090            )
091    private Set<TermSpecificationBo> prerequisites;
092
093    @OneToMany(targetEntity = TermResolverParameterSpecificationBo.class, fetch = FetchType.LAZY, orphanRemoval = true, cascade = { CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.PERSIST })
094    @JoinColumn(name = "TERM_RSLVR_ID", referencedColumnName = "TERM_RSLVR_ID", insertable = false, updatable = false)
095    private Set<TermResolverParameterSpecificationBo> parameterSpecifications;
096
097    @OneToMany(targetEntity = TermResolverAttributeBo.class, fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH })
098    @JoinColumn(name = "TERM_RSLVR_ID", referencedColumnName = "TERM_RSLVR_ID", insertable = false, updatable = false)
099    private Set<TermResolverAttributeBo> attributeBos;
100
101    public void setParameterNames(Set<String> pns) {
102        if (pns != null) {
103            parameterSpecifications = new HashSet<TermResolverParameterSpecificationBo>();
104
105            for (String pn : pns) {
106                TermResolverParameterSpecificationBo paramSpecBo = new TermResolverParameterSpecificationBo();
107                paramSpecBo.setName(pn);
108                paramSpecBo.setTermResolver(this);
109                parameterSpecifications.add(paramSpecBo);
110            }
111        }
112    }
113
114    public Set<String> getParameterNames() {
115        Set<String> results = Collections.emptySet();
116        if (parameterSpecifications != null && parameterSpecifications.size() > 0) {
117            results = new HashSet<String>();
118
119            for (TermResolverParameterSpecificationBo parmSpec : parameterSpecifications) {
120                results.add(parmSpec.getName());
121            }
122        }
123
124        return results;
125    }
126
127    public Map<String, String> getAttributes() {
128        HashMap<String, String> attributes = new HashMap<String, String>();
129        if (attributeBos != null) for (TermResolverAttributeBo attr : attributeBos) {
130            attributes.put(attr.getAttributeDefinition().getName(), attr.getValue());
131        }
132
133        return attributes;
134    }
135
136    /**
137     * Converts a mutable bo to it's immutable counterpart
138     *
139     * @param bo the mutable business object
140     * @return the immutable object
141     */
142    public static TermResolverDefinition to(TermResolverBo bo) {
143        if (bo == null) {
144            return null;
145        }
146
147        return TermResolverDefinition.Builder.create(bo).build();
148    }
149
150    /**
151     * Converts a immutable object to it's mutable bo counterpart
152     *
153     * @param im immutable object
154     * @return the mutable bo
155     */
156    public static TermResolverBo from(TermResolverDefinition im) {
157        if (im == null) {
158            return null;
159        }
160
161        TermResolverBo bo = new TermResolverBo();
162        bo.id = im.getId();
163        bo.namespace = im.getNamespace();
164        bo.name = im.getName();
165        bo.typeId = im.getTypeId();
166        bo.output = TermSpecificationBo.from(im.getOutput());
167        bo.outputId = im.getOutput().getId();
168        bo.setParameterNames(new HashSet<String>());
169
170        for (String paramName : im.getParameterNames()) {
171            TermResolverParameterSpecificationBo parmSpecBo = TermResolverParameterSpecificationBo.from(im, paramName);
172            bo.parameterSpecifications.add(parmSpecBo);
173            parmSpecBo.setTermResolver(bo);
174        }
175
176        bo.prerequisites = new HashSet<TermSpecificationBo>();
177
178        for (TermSpecificationDefinition prereq : im.getPrerequisites()) {
179            bo.prerequisites.add(TermSpecificationBo.from(prereq));
180        }
181
182        // build the set of term resolver attribute BOs
183        Set<TermResolverAttributeBo> attrs = new HashSet<TermResolverAttributeBo>();
184        // for each converted pair, build an TermResolverAttributeBo and add it to the set 
185        TermResolverAttributeBo attributeBo;
186
187        for (Map.Entry<String, String> entry : im.getAttributes().entrySet()) {
188            KrmsAttributeDefinitionBo attrDefBo = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().getKrmsAttributeBo(entry.getKey(), im.getNamespace());
189            attributeBo = new TermResolverAttributeBo();
190            attributeBo.setTermResolverId(im.getId());
191            attributeBo.setValue(entry.getValue());
192            attributeBo.setAttributeDefinition(attrDefBo);
193            ((HashSet<TermResolverAttributeBo>) attrs).add(attributeBo);
194        }
195
196        bo.setAttributeBos(attrs);
197        bo.active = im.isActive();
198        bo.setVersionNumber(im.getVersionNumber());
199
200        return bo;
201    }
202
203    public TermSpecificationBo getOutput() {
204        return output;
205    }
206
207    public String getId() {
208        return id;
209    }
210
211    public void setId(String id) {
212        this.id = id;
213    }
214
215    public String getNamespace() {
216        return namespace;
217    }
218
219    public void setNamespace(String namespace) {
220        this.namespace = namespace;
221    }
222
223    public String getName() {
224        return name;
225    }
226
227    public void setName(String name) {
228        this.name = name;
229    }
230
231    public String getContextId() {
232        return contextId;
233    }
234
235    public void setContextId(String contextId) {
236        this.contextId = contextId;
237    }
238
239    public String getTypeId() {
240        return typeId;
241    }
242
243    public void setTypeId(String typeId) {
244        this.typeId = typeId;
245    }
246
247    public String getOutputId() {
248        return outputId;
249    }
250
251    public void setOutputId(String outputId) {
252        this.outputId = outputId;
253    }
254
255    public boolean getActive() {
256        return active;
257    }
258
259    public boolean isActive() {
260        return active;
261    }
262
263    public void setActive(boolean active) {
264        this.active = active;
265    }
266
267    public Long getVersionNumber() {
268        return versionNumber;
269    }
270
271    public void setVersionNumber(Long versionNumber) {
272        this.versionNumber = versionNumber;
273    }
274
275    public void setOutput(TermSpecificationBo output) {
276        this.output = output;
277    }
278
279    public Set<TermSpecificationBo> getPrerequisites() {
280        return prerequisites;
281    }
282
283    public void setPrerequisites(Set<TermSpecificationBo> prerequisites) {
284        this.prerequisites = prerequisites;
285    }
286
287    public Set<TermResolverParameterSpecificationBo> getParameterSpecifications() {
288        return parameterSpecifications;
289    }
290
291    public void setParameterSpecifications(Set<TermResolverParameterSpecificationBo> parameterSpecifications) {
292        this.parameterSpecifications = parameterSpecifications;
293    }
294
295    public Set<TermResolverAttributeBo> getAttributeBos() {
296        return attributeBos;
297    }
298
299    public void setAttributeBos(Set<TermResolverAttributeBo> attributeBos) {
300        this.attributeBos = attributeBos;
301    }
302}