001/**
002 * Copyright 2005-2013 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.dto;
017
018import org.apache.commons.lang.StringEscapeUtils;
019import org.apache.commons.lang.StringUtils;
020import org.kuali.rice.krms.api.repository.proposition.PropositionDefinitionContract;
021import org.kuali.rice.krms.api.repository.proposition.PropositionParameterContract;
022import org.kuali.rice.krms.api.repository.term.TermDefinition;
023import org.kuali.rice.krms.impl.ui.TermParameter;
024
025import java.io.ByteArrayOutputStream;
026import java.io.OutputStreamWriter;
027import java.io.Serializable;
028import java.nio.charset.Charset;
029import java.util.ArrayList;
030import java.util.HashMap;
031import java.util.List;
032import java.util.Map;
033import java.util.UUID;
034
035/**
036 * @author Kuali Student Team
037 */
038public class PropositionEditor implements PropositionDefinitionContract, Serializable {
039
040    private static final long serialVersionUID = 1L;
041
042    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PropositionEditor.class);
043
044    private String key;
045
046    private String id;
047    private String description;
048    private String ruleId;
049    private String compoundOpCode;
050    private Integer compoundSequenceNumber;
051    private String typeId;
052    private String propositionTypeCode;
053    private Long versionNumber;
054
055    /**Natural Language**/
056    private Map<String, String> naturalLanguage = new HashMap<String, String>();
057
058    private List<PropositionParameterEditor> parameters;
059    private List<PropositionEditor> compoundEditors;
060
061    private TermEditor term;
062    private String termParameter;
063    private List<TermParameter> termParameterList = new ArrayList<TermParameter>();
064    private String type;
065    private boolean editMode = false;
066    private boolean newProp = false;
067
068    private String bindingPath;
069    private String newTermDescription = "new term " + UUID.randomUUID().toString();
070
071    public PropositionEditor() {
072        super();
073    }
074
075    /**
076     * Converts a immutable object to it's mutable bo counterpart
077     *
078     * @param definition immutable object
079     * @return the mutable bo
080     */
081    public PropositionEditor(PropositionDefinitionContract definition) {
082        this.id = definition.getId();
083        this.description = definition.getDescription();
084        this.ruleId = definition.getRuleId();
085
086        this.typeId = definition.getTypeId();
087        this.propositionTypeCode = definition.getPropositionTypeCode();
088        this.parameters = new ArrayList<PropositionParameterEditor>();
089        for (PropositionParameterContract parm : definition.getParameters()) {
090            this.parameters.add(new PropositionParameterEditor(parm));
091        }
092        this.compoundOpCode = definition.getCompoundOpCode();
093        this.compoundSequenceNumber = definition.getCompoundSequenceNumber();
094        this.compoundEditors = new ArrayList<PropositionEditor>();
095        for (PropositionDefinitionContract prop : definition.getCompoundComponents()) {
096            this.compoundEditors.add(createPropositionEditor(prop));
097        }
098        this.versionNumber = definition.getVersionNumber();
099    }
100
101    public void clear(){
102        this.description = null;
103        this.term = null;
104        this.termParameter = null;
105        for (PropositionParameterEditor parm : this.getParameters()) {
106            parm.clear();
107        }
108    }
109
110    public String getKey() {
111        return key;
112    }
113
114    public void setKey(String key) {
115        this.key = key;
116    }
117
118    public String getId() {
119        return id;
120    }
121
122    public String getDescription() {
123        return description;
124    }
125
126    public String getCompoundOpCode() {
127        return compoundOpCode;
128    }
129    
130    @Override
131    public Integer getCompoundSequenceNumber() {
132        return compoundSequenceNumber;
133    }
134
135    public void setId(String id) {
136        if (!StringUtils.isBlank(id) || id == null) {
137            this.id = id;
138        }
139    }
140
141    public void setDescription(String description) {
142        //Description can only handle 100 characters.
143        if ((description != null) && (description.length()>100)) {
144            description = description.substring(0,97) + "...";
145        }
146        this.description = description;
147        LOG.info(this.description);
148    }
149
150    public void setRuleId(String ruleId) {
151        this.ruleId = ruleId;
152    }
153
154    public void setCompoundOpCode(String compoundOpCode) {
155        this.compoundOpCode = compoundOpCode;
156    }
157
158    public void setParameters(List<PropositionParameterEditor> parameters) {
159        this.parameters = parameters;
160    }
161
162    public void setCompoundEditors(List<PropositionEditor> compoundEditors) {
163        this.compoundEditors = compoundEditors;
164    }
165
166    public List<PropositionEditor> getCompoundEditors() {
167        return compoundEditors;
168    }
169
170    @Override
171    public List<? extends PropositionDefinitionContract> getCompoundComponents() {
172        return compoundEditors;
173    }
174
175    public void setVersionNumber(Long versionNumber) {
176        this.versionNumber = versionNumber;
177    }
178
179    public List<PropositionParameterEditor> getParameters() {
180        return parameters;
181    }
182
183    public String getTypeId() {
184        return typeId;
185    }
186
187    @Override
188    public String getRuleId() {
189        return this.ruleId;
190    }
191
192    public void setPropositionTypeCode(String propositionTypeCode) {
193        this.propositionTypeCode = propositionTypeCode;
194    }
195
196    @Override
197    public String getPropositionTypeCode() {
198        return propositionTypeCode;
199    }
200
201    public void setTypeId(String typeId) {
202        this.typeId = typeId;
203    }
204
205    public String getType() {
206        return type;
207    }
208
209    public void setType(String type) {
210        this.type = type;
211    }
212
213    public boolean isEditMode() {
214        return editMode;
215    }
216
217    public TermEditor getTerm() {
218        return term;
219    }
220
221    public void setTerm(TermEditor term) {
222        this.term = term;
223    }
224
225    public String getTermParameter() {
226        return termParameter;
227    }
228
229    public void setTermParameter(String termParameter) {
230        this.termParameter = termParameter;
231
232        if(this.termParameter!=null){
233            LOG.info(this.termParameter);
234
235            //This is just temp code to prove what the actual problem is.
236            this.termParameter.replaceAll("\\u00a0","");
237        }
238        LOG.info(termParameter);
239    }
240
241    public List<TermParameter> getTermParameterList() {
242        return termParameterList;
243    }
244
245    public void setTermParameterList(List<TermParameter> termParameterList) {
246        this.termParameterList = termParameterList;
247    }
248
249    public void setEditMode(boolean editMode) {
250        this.editMode = editMode;
251    }
252
253    @Override
254    public Long getVersionNumber() {
255        return this.versionNumber;
256    }
257
258    protected PropositionEditor createPropositionEditor(PropositionDefinitionContract definition) {
259        return new PropositionEditor(definition);
260    }
261
262    public String getNewTermDescription() {
263        return newTermDescription;
264    }
265
266    public boolean isNewProp() {
267        return newProp;
268    }
269
270    public void setNewProp(boolean newProp) {
271        this.newProp = newProp;
272    }
273
274    public Map<String, String> getNaturalLanguage() {
275        return naturalLanguage;
276    }
277
278    public void setNaturalLanguage(Map<String, String> naturalLanguage) {
279        this.naturalLanguage = naturalLanguage;
280    }
281
282    /**
283     * Return the natural language description for the given usage key from the natural language map.
284     *
285     * @param usage
286     * @return
287     */
288    public String getNaturalLanguageForUsage(String usage){
289        String description = this.getNaturalLanguage().get(usage);
290
291        if (description == null){
292            return StringUtils.EMPTY;
293        }
294
295        return description;
296    }
297
298    /**
299     * Set the natuaral language string on the map with the usage as key. If the usage is the default
300     * usage also set the description of the proposition.
301     *
302     * @param usage
303     * @param nl
304     */
305    public void setNaturalLanguageForUsage(String usage, String nl){
306        this.getNaturalLanguage().put(usage, nl);
307
308        if (usage.equals(this.getDefaultNlKey())){
309            this.setDescription(nl);
310        }
311    }
312
313    /**
314     * Override this method to return a method of custom parameters to be used in the natural
315     * language context implementation.
316     *
317     * @return
318     */
319    public Map<String, String> getNlParameters() {
320        return new HashMap<String, String>();
321    }
322
323    public String getBindingPath() {
324        return bindingPath;
325    }
326
327    public void setBindingPath(String bindingPath) {
328        this.bindingPath = bindingPath;
329    }
330
331    protected String getDefaultNlKey(){
332        return null;
333    }
334}
335
336