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.api.repository;
017
018import org.kuali.rice.core.api.CoreConstants;
019import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
020import org.kuali.rice.core.api.mo.ModelBuilder;
021import org.kuali.rice.krms.api.KrmsConstants;
022import org.w3c.dom.Element;
023
024import javax.xml.bind.annotation.XmlAccessType;
025import javax.xml.bind.annotation.XmlAccessorType;
026import javax.xml.bind.annotation.XmlAnyElement;
027import javax.xml.bind.annotation.XmlElement;
028import javax.xml.bind.annotation.XmlRootElement;
029import javax.xml.bind.annotation.XmlType;
030import java.io.Serializable;
031import java.util.Collection;
032
033/**
034 * Concrete model object implementation of a natural language tree immutable.
035 * Instances of natural language tree can be (un)marshalled to and from XML.
036 *
037 * @see NaturalLanguageContract
038 */
039@XmlRootElement(name = NaturalLanguage.Constants.ROOT_ELEMENT_NAME)
040@XmlAccessorType(XmlAccessType.NONE)
041@XmlType(name = NaturalLanguage.Constants.TYPE_NAME, propOrder = {
042        NaturalLanguage.Elements.KRMS_OBJECT_ID,
043        NaturalLanguage.Elements.NATURAL_LANGUAGE,
044        CoreConstants.CommonElements.FUTURE_ELEMENTS
045})
046public class NaturalLanguage extends AbstractDataTransferObject implements NaturalLanguageContract {
047
048    private static final long serialVersionUID = 6112385261827152719L;
049
050    @XmlElement(name = NaturalLanguage.Elements.KRMS_OBJECT_ID, required = false)
051    private String krmsObjectId;
052    @XmlElement(name = NaturalLanguage.Elements.NATURAL_LANGUAGE, required = false)
053    private String naturalLanguage;
054    @SuppressWarnings("unused")
055    @XmlAnyElement
056    private final Collection<Element> _futureElements = null;
057
058    /**
059     * This constructor should never be called. It is only present for use
060     * during JAXB unmarshalling.
061     */
062    public NaturalLanguage() {
063        this.krmsObjectId = null;
064        this.naturalLanguage = null;
065    }
066
067    @Override
068    public String getKrmsObjectId() {
069        return krmsObjectId;
070    }
071
072    @Override
073    public String getNaturalLanguage() {
074        return naturalLanguage;
075    }
076
077    /**
078     * Constructs a KRMS Repository Agenda object from the given builder. This
079     * constructor is private and should only ever be invoked from the builder.
080     *
081     * @param builder the Builder from which to construct the Agenda
082     */
083    private NaturalLanguage(Builder builder) {
084        this.krmsObjectId = builder.getKrmsObjectId();
085        this.naturalLanguage = builder.getNaturalLanguage();
086    }
087
088    /**
089     * This builder is used to construct instances of KRMS Repository Agenda. It
090     * enforces the constraints of the {@link NaturalLanguageContract}.
091     */
092    public static class Builder implements NaturalLanguageContract, ModelBuilder, Serializable {
093
094        private static final long serialVersionUID = -2354545388959879306L;
095
096        private String krmsObjectId;
097        private String naturalLanguage;
098
099        /**
100         * Private constructor for creating a builder with all of it's required
101         * attributes.
102         */
103        private Builder() {
104        }
105
106        /**
107         * Private constructor for creating a builder with all of it's required
108         * attributes.
109         */
110        private Builder(String naturalLanguage, String krmsObjectId) {
111            setNaturalLanguage(naturalLanguage);
112            setKrmsObjectId(krmsObjectId);
113        }
114
115        @Override
116        public String getKrmsObjectId() {
117            return krmsObjectId;
118        }
119
120        public void setKrmsObjectId(String krmsObjectId) {
121            this.krmsObjectId = krmsObjectId;
122        }
123
124        @Override
125        public String getNaturalLanguage() {
126            return naturalLanguage;
127        }
128
129        public void setNaturalLanguage(String naturalLanguage) {
130            this.naturalLanguage = naturalLanguage;
131        }
132
133        /**
134         * Creates a builder by populating it with data from the given
135         * {@link NaturalLanguageContract}.
136         *
137         * @return an instance of the builder populated with data from the
138         * contract
139         * @throws IllegalArgumentException if the contract is null
140         */
141        public static NaturalLanguage.Builder create() {
142            NaturalLanguage.Builder builder = new NaturalLanguage.Builder();
143            return builder;
144        }
145        /**
146         * Creates a builder by populating it with data from the given
147         * {@link NaturalLanguageContract}.
148         *
149         * @param contract the contract from which to populate this builder
150         * @return an instance of the builder populated with data from the
151         * contract
152         * @throws IllegalArgumentException if the contract is null
153         */
154        public static NaturalLanguage.Builder create(NaturalLanguageContract contract) {
155            if (contract == null) {
156                throw new IllegalArgumentException("contract is null");
157            }
158            NaturalLanguage.Builder builder = new NaturalLanguage.Builder(contract.getNaturalLanguage(), contract.getKrmsObjectId());
159
160            return builder;
161        }
162
163        /**
164         * Builds an instance of a Natural Language Tree based on the current state of the
165         * builder.
166         *
167         * @return the fully-constructed Agenda
168         */
169        @Override
170        public NaturalLanguage build() {
171            return new NaturalLanguage(this);
172        }
173    }
174
175    /**
176     * Defines some constants used on this class.
177     */
178    public static class Constants {
179
180        final static String ROOT_ELEMENT_NAME = "naturalLanguage";
181        final static String TYPE_NAME = "NaturalLanguageType";
182        final static String[] HASH_CODE_EQUALS_EXCLUDE = {"_futureElements"};
183        public final static String EVENT = "Event";   // key for event attribute
184    }
185
186    /**
187     * A private class which exposes constants which define the XML element
188     * names to use when this object is marshalled to XML.
189     */
190    public static class Elements {
191
192        final static String KRMS_OBJECT_ID = "krmsObjectId";
193        final static String NATURAL_LANGUAGE = "naturalLanguage";
194    }
195
196    public static class Cache {
197
198        public static final String NAME = KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0 + "/" + NaturalLanguage.Constants.TYPE_NAME;
199    }
200}