001/**
002 * Copyright 2005-2016 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.exception.RiceIllegalStateException;
019import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
020import org.kuali.rice.krad.service.KRADServiceLocator;
021import org.kuali.rice.krad.service.SequenceAccessorService;
022import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplate;
023import org.kuali.rice.krms.api.repository.language.NaturalLanguageTemplateContract;
024import org.kuali.rice.krms.api.repository.type.KrmsAttributeDefinition;
025import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
026import org.kuali.rice.krms.api.repository.type.KrmsTypeRepositoryService;
027
028import java.util.Collection;
029import java.util.Collections;
030import java.util.HashMap;
031import java.util.HashSet;
032import java.util.LinkedList;
033import java.util.List;
034import java.util.Map;
035import java.util.Set;
036
037/**
038 * The mutable implementation of the @{link NaturalLanguageTemplateContract} interface, the counterpart to the immutable implementation {@link NaturalLanguageTemplate}.
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 * 
041 */
042public class NaturalLanguageTemplateBo
043    extends PersistableBusinessObjectBase
044    implements NaturalLanguageTemplateContract
045{
046
047    private Map<String, String> attributes;
048    private String languageCode;
049    private String naturalLanguageUsageId;
050    private String typeId;
051    private String template;
052    private String id;
053    private boolean active;
054    private Long versionNumber;
055    private SequenceAccessorService sequenceAccessorService;
056    private Set<NaturalLanguageTemplateAttributeBo> attributeBos;
057    private static KrmsAttributeDefinitionService attributeDefinitionService;
058    private static KrmsTypeRepositoryService typeRepositoryService;
059
060    /**
061     * Default Constructor
062     * 
063     */
064    public NaturalLanguageTemplateBo() {
065    }
066
067    @Override
068    public String getLanguageCode() {
069        return this.languageCode;
070    }
071
072    @Override
073    public String getNaturalLanguageUsageId() {
074        return this.naturalLanguageUsageId;
075    }
076
077    @Override
078    public String getTypeId() {
079        return this.typeId;
080    }
081
082    @Override
083    public String getTemplate() {
084        return this.template;
085    }
086
087    @Override
088    public String getId() {
089        return this.id;
090    }
091
092    @Override
093    public boolean isActive() {
094        return this.active;
095    }
096
097    @Override
098    public Long getVersionNumber() {
099        return this.versionNumber;
100    }
101
102    /**
103     * Sets the value of languageCode on this builder to the given value.
104     * 
105     * @param languageCode the languageCode value to set.
106     * 
107     */
108    public void setLanguageCode(String languageCode) {
109        this.languageCode = languageCode;
110    }
111
112    /**
113     * Sets the value of naturalLanguageUsageId on this builder to the given value.
114     * 
115     * @param naturalLanguageUsageId the naturalLanguageUsageId value to set.
116     * 
117     */
118    public void setNaturalLanguageUsageId(String naturalLanguageUsageId) {
119        this.naturalLanguageUsageId = naturalLanguageUsageId;
120    }
121
122    /**
123     * Sets the value of typeId on this builder to the given value.
124     * 
125     * @param typeId the typeId value to set.
126     * 
127     */
128    public void setTypeId(String typeId) {
129        this.typeId = typeId;
130    }
131
132    /**
133     * Sets the value of template on this builder to the given value.
134     * 
135     * @param template the template value to set.
136     * 
137     */
138    public void setTemplate(String template) {
139        this.template = template;
140    }
141
142    /**
143     * Sets the value of id on this builder to the given value.
144     * 
145     * @param id the id value to set.
146     * 
147     */
148    public void setId(String id) {
149        this.id = id;
150    }
151
152    /**
153     * Sets the value of active on this builder to the given value.
154     * 
155     * @param active the active value to set.
156     * 
157     */
158    public void setActive(boolean active) {
159        this.active = active;
160    }
161
162    /**
163     * Sets the value of versionNumber on this builder to the given value.
164     * 
165     * @param versionNumber the versionNumber value to set.
166     * 
167     */
168    public void setVersionNumber(Long versionNumber) {
169        this.versionNumber = versionNumber;
170    }
171
172    /**
173     * Sets the value of AttributeBos on this builder to the given value.
174     * 
175     * @param attributeBos the AttributeBos value to set.
176     * 
177     */
178    public void setAttributeBos(List<NaturalLanguageTemplateAttributeBo> attributeBos) {
179        this.attributeBos = new HashSet<NaturalLanguageTemplateAttributeBo>(attributeBos);
180    }
181
182    /**
183     * Sets the value of AttributeBos on this builder to the given value.
184     * 
185     * @param attributeBos the AttributeBos value to set.
186     * 
187     */
188    public void setAttributeBos(Set<NaturalLanguageTemplateAttributeBo> attributeBos) {
189        this.attributeBos = new HashSet<NaturalLanguageTemplateAttributeBo>(attributeBos);
190    }
191
192    /**
193     * Converts a mutable {@link NaturalLanguageTemplateBo} to its immutable counterpart, {@link NaturalLanguageTemplate}.
194     * @param naturalLanguageTemplateBo the mutable business object.
195     * @return a {@link NaturalLanguageTemplate} the immutable object.
196     * 
197     */
198    public static NaturalLanguageTemplate to(NaturalLanguageTemplateBo naturalLanguageTemplateBo) {
199        if (naturalLanguageTemplateBo == null) { return null; }
200        return NaturalLanguageTemplate.Builder.create(naturalLanguageTemplateBo).build();
201    }
202
203    /**
204     * Converts a immutable {@link NaturalLanguageTemplate} to its mutable {@link NaturalLanguageTemplateBo} counterpart.
205     * @param naturalLanguageTemplate the immutable object.
206     * @return a {@link NaturalLanguageTemplateBo} the mutable NaturalLanguageTemplateBo.
207     * 
208     */
209    public static org.kuali.rice.krms.impl.repository.NaturalLanguageTemplateBo from(NaturalLanguageTemplate naturalLanguageTemplate) {
210        if (naturalLanguageTemplate == null) return null;
211        NaturalLanguageTemplateBo naturalLanguageTemplateBo = new NaturalLanguageTemplateBo();
212        naturalLanguageTemplateBo.setLanguageCode(naturalLanguageTemplate.getLanguageCode());
213        naturalLanguageTemplateBo.setNaturalLanguageUsageId(naturalLanguageTemplate.getNaturalLanguageUsageId());
214        naturalLanguageTemplateBo.setTypeId(naturalLanguageTemplate.getTypeId());
215        naturalLanguageTemplateBo.setTemplate(naturalLanguageTemplate.getTemplate());
216        naturalLanguageTemplateBo.setId(naturalLanguageTemplate.getId());
217        naturalLanguageTemplateBo.setActive(naturalLanguageTemplate.isActive());
218        naturalLanguageTemplateBo.setVersionNumber(naturalLanguageTemplate.getVersionNumber());
219        // TODO collections, etc.
220        naturalLanguageTemplateBo.setAttributeBos(buildAttributeBoSet(naturalLanguageTemplate));
221        //naturalLanguageTemplateBo.setAttributeBos(buildAttributeBoList(naturalLanguageTemplate));
222        return naturalLanguageTemplateBo;
223    }
224
225    /**
226     * Returns the next available id for the given table and class.
227     * @return String the next available id for the given table and class.
228     * 
229     */
230    private String getNewId(String table, Class clazz) {
231        if (sequenceAccessorService == null) {
232            sequenceAccessorService = KRADServiceLocator.getSequenceAccessorService();
233        }
234        Long id = sequenceAccessorService.getNextAvailableSequenceNumber(table, clazz);
235        return id.toString();
236    }
237
238    /**
239     * Set the SequenceAccessorService, useful for testing.
240     * @param sas SequenceAccessorService to use for getNewId.
241     * 
242     */
243    public void setSequenceAccessorService(SequenceAccessorService sas) {
244        sequenceAccessorService = sas;
245    }
246
247    public SequenceAccessorService getSequenceAccessorService() {
248        return sequenceAccessorService;
249    }
250
251    @Override
252    public Map<String, String> getAttributes() {
253        if (attributeBos == null) return Collections.emptyMap();
254
255        HashMap<String, String> attributes = new HashMap<String, String>(attributeBos.size());
256        for (NaturalLanguageTemplateAttributeBo attr: attributeBos) {
257            attributes.put(attr.getAttributeDefinition().getName(), attr.getValue());
258        }
259        return attributes;
260    }
261
262    /**
263     * TODO
264     * 
265     */
266    public void setAttributes(Map<String, String> attributes) {
267        this.attributeBos  = new HashSet<NaturalLanguageTemplateAttributeBo>();
268        if (!org.apache.commons.lang.StringUtils.isBlank(this.typeId)) {
269            List<KrmsAttributeDefinition> attributeDefinitions = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService().findAttributeDefinitionsByType(this.getTypeId());
270            Map<String, KrmsAttributeDefinition> attributeDefinitionsByName = new HashMap<String, KrmsAttributeDefinition>(attributeDefinitions.size());
271            if (attributeDefinitions != null) for (KrmsAttributeDefinition attributeDefinition : attributeDefinitions) {
272                attributeDefinitionsByName.put(attributeDefinition.getName(), attributeDefinition);
273            }
274            for (Map.Entry<String, String> attr : attributes.entrySet()) {
275                KrmsAttributeDefinition attributeDefinition = attributeDefinitionsByName.get(attr.getKey());
276                NaturalLanguageTemplateAttributeBo attributeBo = new NaturalLanguageTemplateAttributeBo();
277                attributeBo.setNaturalLanguageTemplateId(this.getId());
278                attributeBo.setAttributeDefinitionId((attributeDefinition == null) ? null : attributeDefinition.getId());
279                attributeBo.setValue(attr.getValue());
280                attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attributeDefinition));
281                attributeBos.add(attributeBo);
282            }
283        }
284    }
285
286    private static Collection<NaturalLanguageTemplateAttributeBo> buildAttributes(NaturalLanguageTemplate im, Collection<NaturalLanguageTemplateAttributeBo> attributes) {
287
288        KrmsTypeDefinition krmsTypeDefinition = getTypeRepositoryService().getTypeById(im.getTypeId());
289
290        // for each entry, build a NaturalLanguageTemplateAttributeBo and add it
291        if (im.getAttributes() != null) {
292            for (Map.Entry<String,String> entry  : im.getAttributes().entrySet()) {
293
294                KrmsAttributeDefinition attrDef =
295                        getAttributeDefinitionService().getAttributeDefinitionByNameAndNamespace(entry.getKey(),
296                                krmsTypeDefinition.getNamespace());
297
298                if (attrDef != null) {
299                    NaturalLanguageTemplateAttributeBo attributeBo = new NaturalLanguageTemplateAttributeBo();
300                    attributeBo.setNaturalLanguageTemplateId( im.getId() );
301                    attributeBo.setAttributeDefinitionId(attrDef.getId());
302                    attributeBo.setValue(entry.getValue());
303                    attributeBo.setAttributeDefinition(KrmsAttributeDefinitionBo.from(attrDef));
304                    attributes.add(attributeBo);
305                } else {
306                    throw new RiceIllegalStateException("there is no attribute definition with the name '" +
307                                 entry.getKey() + "' that is valid for the naturalLanguageTemplate type with id = '" + im.getTypeId() +"'");
308                }
309            }
310        }
311        return attributes;
312    }
313
314    private static Set<NaturalLanguageTemplateAttributeBo> buildAttributeBoSet(NaturalLanguageTemplate im) {
315        Set<NaturalLanguageTemplateAttributeBo> attributes = new HashSet<NaturalLanguageTemplateAttributeBo>();
316        return (Set)buildAttributes(im, attributes);
317    }
318
319    private static List<NaturalLanguageTemplateAttributeBo> buildAttributeBoList(NaturalLanguageTemplate im) {
320        List<NaturalLanguageTemplateAttributeBo> attributes = new LinkedList<NaturalLanguageTemplateAttributeBo>();
321        return (List)buildAttributes(im, attributes);
322    }
323
324    public static void setAttributeDefinitionService(KrmsAttributeDefinitionService attributeDefinitionService) {
325        NaturalLanguageTemplateBo.attributeDefinitionService = attributeDefinitionService; // TODO gen
326    }
327
328    public static KrmsTypeRepositoryService getTypeRepositoryService() {
329        if (typeRepositoryService == null) {
330            typeRepositoryService = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService();
331        }
332        return typeRepositoryService;
333    }
334
335    public static void setTypeRepositoryService(KrmsTypeRepositoryService typeRepositoryService) {
336        NaturalLanguageTemplateBo.typeRepositoryService = typeRepositoryService;
337    }
338
339    public static KrmsAttributeDefinitionService getAttributeDefinitionService() {
340        if (attributeDefinitionService == null) {
341            attributeDefinitionService = KrmsRepositoryServiceLocator.getKrmsAttributeDefinitionService();
342        }
343        return attributeDefinitionService;
344    }
345
346}