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     */
016    package org.kuali.rice.krms.api.repository.term;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.CoreConstants;
020    import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021    import org.kuali.rice.core.api.mo.ModelBuilder;
022    import org.kuali.rice.core.api.util.jaxb.MapStringStringAdapter;
023    import org.kuali.rice.krms.api.KrmsConstants;
024    import org.kuali.rice.krms.api.repository.BuilderUtils;
025    
026    import javax.xml.bind.annotation.XmlAccessType;
027    import javax.xml.bind.annotation.XmlAccessorType;
028    import javax.xml.bind.annotation.XmlAnyElement;
029    import javax.xml.bind.annotation.XmlElement;
030    import javax.xml.bind.annotation.XmlElementWrapper;
031    import javax.xml.bind.annotation.XmlRootElement;
032    import javax.xml.bind.annotation.XmlType;
033    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
034    import java.io.Serializable;
035    import java.util.Collection;
036    import java.util.Collections;
037    import java.util.Map;
038    import java.util.Set;
039    
040    /**
041     * This is a description of what this class does - gilesp don't forget to fill this in. 
042     * 
043     * @author Kuali Rice Team (rice.collab@kuali.org)
044     *
045     */
046    @XmlRootElement(name = TermResolverDefinition.Constants.ROOT_ELEMENT_NAME)
047    @XmlAccessorType(XmlAccessType.NONE)
048    @XmlType(name = TermResolverDefinition.Constants.TYPE_NAME, propOrder = {
049                    TermResolverDefinition.Elements.ID,
050                    TermResolverDefinition.Elements.NAME,
051            TermResolverDefinition.Elements.NAMESPACE,
052                    TermResolverDefinition.Elements.TYPE_ID,
053            TermResolverDefinition.Elements.ACTIVE,
054                    TermResolverDefinition.Elements.OUTPUT,
055                    TermResolverDefinition.Elements.PREREQUISITES,
056                    TermResolverDefinition.Elements.ATTRIBUTES,
057                    TermResolverDefinition.Elements.PARAMETER_NAMES,
058            CoreConstants.CommonElements.VERSION_NUMBER,
059                    CoreConstants.CommonElements.FUTURE_ELEMENTS
060    })
061    public final class TermResolverDefinition extends AbstractDataTransferObject implements TermResolverDefinitionContract {
062            
063        private static final long serialVersionUID = 1L;
064            
065            @XmlElement(name = Elements.ID, required=false)
066            private final String id;
067            @XmlElement(name = Elements.NAMESPACE, required=true)
068            private final String namespace;
069            @XmlElement(name = Elements.NAME, required=true)
070            private final String name;
071        @XmlElement(name = Elements.TYPE_ID, required=true)
072        private final String typeId;
073            @XmlElement(name = Elements.OUTPUT, required=false)
074            private final TermSpecificationDefinition output;
075        @XmlElement(name = Elements.ACTIVE, required = false)
076        private final boolean active;
077    
078            @XmlElement(name = "termSpecificationDefinition", required=false)
079            @XmlElementWrapper(name = Elements.PREREQUISITES, required=false)
080            private final Set<TermSpecificationDefinition> prerequisites;
081    
082            @XmlElement(name = Elements.ATTRIBUTES, required = false)
083            @XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
084            private final Map<String, String> attributes;
085    
086            @XmlElementWrapper(name = Elements.PARAMETER_NAMES, required=false)
087            @XmlElement(name = "parameterName")
088            private final Set<String> parameterNames;
089    
090            @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
091        private final Long versionNumber;   
092            
093        @SuppressWarnings("unused")
094            @XmlAnyElement
095            private final Collection<org.w3c.dom.Element> _futureElements = null;
096        
097            /**
098             * This private constructor is for JAXB use only, don't invoke directly.
099             */
100            private TermResolverDefinition() {
101                    id = null;
102                    namespace = null;
103                    name = null;
104                    typeId = null;
105            active = true;
106                    output = null;
107                    prerequisites = null;
108                    attributes = null;
109                    parameterNames = null;
110            versionNumber = null;
111            }
112            
113            private TermResolverDefinition(Builder builder) {
114                    this.id = builder.getId();
115                    this.namespace = builder.getNamespace();
116                    this.name = builder.getName();
117                    this.typeId = builder.getTypeId();
118            this.active = builder.isActive();
119                    this.output = builder.getOutput().build();
120                    this.prerequisites = BuilderUtils.convertFromBuilderSet(builder.getPrerequisites());
121                    this.parameterNames = Collections.unmodifiableSet(builder.getParameterNames());
122                    this.versionNumber = builder.getVersionNumber();
123            if (builder.attributes != null){
124                    this.attributes = Collections.unmodifiableMap(builder.getAttributes());
125            } else {
126                    this.attributes = null;
127            }
128            }
129            
130            public static class Builder implements TermResolverDefinitionContract, ModelBuilder, 
131                    Serializable {
132                    
133                    private static final long serialVersionUID = 1L;
134                    
135                    private String id;
136                    private String namespace;
137                    private String name;
138            private String typeId;
139            private boolean active;
140                    private TermSpecificationDefinition.Builder output;
141                    private Set<TermSpecificationDefinition.Builder> prerequisites;
142            private Map<String, String> attributes;
143                    private Set<String> parameterNames;
144            private Long versionNumber;
145                    
146                    private Builder(String id, String namespaceCode, String name, String typeId, TermSpecificationDefinition.Builder output,
147                    Set<TermSpecificationDefinition.Builder> prerequisites, Map<String, String> attributes, Set<String> parameterNames) {
148                            setId(id);
149                            setNamespace(namespaceCode);
150                            setName(name);
151                            setTypeId(typeId);
152                setActive(true);
153                            setOutput(output);
154                            setPrerequisites(prerequisites);
155                            setAttributes(attributes);
156                            setParameterNames(parameterNames);
157                    }
158                    
159                    
160                    
161                    private Builder(TermResolverDefinitionContract termResolver) {
162                            setId(termResolver.getId());
163                            setNamespace(termResolver.getNamespace());
164                            setName(termResolver.getName());
165                            setTypeId(termResolver.getTypeId());
166                setActive(termResolver.isActive());
167                            setOutput(TermSpecificationDefinition.Builder.create(termResolver.getOutput()));
168                            setPrerequisites(BuilderUtils.transform(termResolver.getPrerequisites(), TermSpecificationDefinition.Builder.toBuilder));
169                            setAttributes(termResolver.getAttributes());
170                            this.setParameterNames(termResolver.getParameterNames());
171                            this.setVersionNumber(termResolver.getVersionNumber());
172                    }
173                    
174                    public static Builder create(TermResolverDefinitionContract termResolver) {
175                            return new Builder(termResolver);
176                    }
177                    
178                    public static Builder create(String id, String namespaceCode, String name, String typeId,
179                    TermSpecificationDefinition.Builder output, Set<TermSpecificationDefinition.Builder> prerequisites, Map<String, String> attributes,
180                    Set<String> parameterNames) {
181                            return new Builder(id, namespaceCode, name, typeId, output, prerequisites, attributes, parameterNames);
182                    }
183    
184                    // Builder setters:
185                    // TODO: proper validation & javadocs
186                    
187                    /**
188                     * @param id the id to set
189                     */
190                    public void setId(String id) {
191                            if (id != null && StringUtils.isBlank(id)) {
192                                    throw new IllegalArgumentException(/* TODO */);
193                            }
194                            this.id = id;
195                    }
196    
197                    /**
198                     * @param namespace the namespace to set
199                     */
200                    public void setNamespace(String namespace) {
201                            if (StringUtils.isBlank(namespace)) {
202                                    throw new IllegalArgumentException(/* TODO */);
203                            }
204                            this.namespace = namespace;
205                    }
206    
207                    /**
208                     * @param name the name to set
209                     */
210                    public void setName(String name) {
211                            if (StringUtils.isBlank(name)) {
212                                    throw new IllegalArgumentException(/* TODO */);
213                            }
214                            this.name = name;
215                    }
216    
217                    /**
218                     * @param typeId the typeId to set
219                     */
220                    public void setTypeId(String typeId) {
221                            if (StringUtils.isBlank(typeId)) {
222                                    throw new IllegalArgumentException(/* TODO */);
223                            }
224                            this.typeId = typeId;
225                    }
226    
227            /**
228             * @param active the active indicator
229             */
230            public void setActive(boolean active) {
231                this.active = active;
232            }
233    
234                    /**
235                     * @param output the output to set
236                     */
237                    public void setOutput(TermSpecificationDefinition.Builder output) {
238                            if (output == null) {
239                                    throw new IllegalArgumentException(/* TODO */);
240                            }
241                            this.output = output;
242                    }
243    
244                    /**
245                     * @param prerequisites the prerequisites to set
246                     */
247                    public void setPrerequisites(
248                                    Set<TermSpecificationDefinition.Builder> prerequisites) {
249                            this.prerequisites = prerequisites;
250                    }
251    
252                    /**
253                     * @param attributes the attributes to set
254                     */
255                    public void setAttributes(Map<String, String> attributes){
256                            if (attributes == null){
257                                    this.attributes = Collections.emptyMap();
258                            } else {
259                                    this.attributes = Collections.unmodifiableMap(attributes);
260                            }
261                    }
262    
263                    /**
264                     * @param parameterNames the parameterNames to set
265                     */
266                    public void setParameterNames(Set<String> parameterNames) {
267                            this.parameterNames = parameterNames;
268                    }               
269                    
270                    /**
271                     * @param versionNumber the versionNumber to set.  May be null.
272                     */
273            public void setVersionNumber(Long versionNumber){
274                this.versionNumber = versionNumber;
275            }
276            
277                    // Builder getters:
278    
279                    /**
280                     * @return the id
281                     */
282                    public String getId() {
283                            return this.id;
284                    }
285                    
286                    /**
287                     * @return the namespace
288                     */
289                    public String getNamespace() {
290                            return this.namespace;
291                    }
292                    /**
293                     * @return the name
294                     */
295                    public String getName() {
296                            return this.name;
297                    }
298                    /**
299                     * @return the typeId
300                     */
301                    public String getTypeId() {
302                            return this.typeId;
303                    }
304            /**
305             * @return the active indicator
306             */
307            public boolean isActive() {
308                return this.active;
309            }
310                    /**
311                     * @return the output
312                     */
313                    public TermSpecificationDefinition.Builder getOutput() {
314                            return this.output;
315                    }
316                    /**
317                     * @return the prerequisites
318                     */
319                    public Set<TermSpecificationDefinition.Builder> getPrerequisites() {
320                            return this.prerequisites;
321                    }
322                    /**
323                     * @return the attributes
324                     */
325                    public Map<String, String> getAttributes() {
326                            return this.attributes;
327                    }
328                    /**
329                     * @return the parameterNames
330                     */
331                    public Set<String> getParameterNames() {
332                            return (parameterNames == null) ? Collections.<String>emptySet() : parameterNames;
333                    }
334                    
335                    /**
336                     * @return the version number
337                     */
338            @Override
339            public Long getVersionNumber() {
340                return this.versionNumber;
341            }
342    
343                    /**
344                     * This overridden method ...
345                     * 
346                     * @see org.kuali.rice.core.api.mo.ModelBuilder#build()
347                     */
348                    @Override
349                    public TermResolverDefinition build() {
350                            return new TermResolverDefinition(this);
351                    }
352                    
353            }
354            
355            /**
356             * @return the id
357             */
358            @Override
359            public String getId() {
360                    return this.id;
361            }
362            
363            /**
364             * @return the namespace
365             */
366            @Override
367            public String getNamespace() {
368                    return this.namespace;
369            }
370            
371            /**
372             * @return the name
373             */
374            @Override
375            public String getName() {
376                    return this.name;
377            }
378            
379            /**
380             * @return the typeId
381             */
382            @Override
383            public String getTypeId() {
384                    return this.typeId;
385            }
386    
387        /**
388         * @return the active indicator
389         */
390        @Override
391        public boolean isActive() {
392            return active;
393        }
394    
395            /**
396             * @return the specification
397             */
398            @Override
399            public TermSpecificationDefinition getOutput() {
400                    return this.output;
401            }
402            
403            /**
404             * @return the prerequisites
405             */
406            @Override
407            public Set<TermSpecificationDefinition> getPrerequisites() {
408                    return this.prerequisites;
409            }
410            /**
411             * @return the attributes
412             */
413            @Override
414            public Map<String, String> getAttributes() {
415                    return this.attributes;
416            }
417    
418            /**
419             * @return the parameterNames
420             */
421            @Override
422            public Set<String> getParameterNames() {
423                    return this.parameterNames;
424            }
425            
426            /**
427             * @see org.kuali.rice.core.api.mo.common.Versioned#getVersionNumber()
428             */
429        @Override
430        public Long getVersionNumber() {
431            return versionNumber;
432        }
433            
434            /**
435             * Defines some internal constants used on this class.
436             */
437            static class Constants {
438                    final static String ROOT_ELEMENT_NAME = "termResolverDefinition";
439                    final static String TYPE_NAME = "termResolverDefinitionType";
440            }
441            
442            static class Elements {
443                    public static final String ID = "id";
444                    public static final String NAMESPACE = "namespace";
445                    public static final String NAME = "name";
446            public static final String TYPE_ID = "typeId";
447                    public static final String OUTPUT = "output";
448                    public static final String PREREQUISITES = "prerequisites";
449                    public static final String ATTRIBUTES = "attributes";
450                    public static final String PARAMETER_NAMES = "parameterNames";
451            public static final String ACTIVE = "active";
452            }
453    
454        public static class Cache {
455            public static final String NAME = KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0 + "/" + TermResolverDefinition.Constants.TYPE_NAME;
456        }
457    }