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