001/**
002 * Copyright 2005-2015 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.kew.api.document.attribute;
017
018import org.joda.time.DateTime;
019
020import java.math.BigDecimal;
021import java.math.BigInteger;
022import java.util.Calendar;
023import java.util.Date;
024
025/**
026 * A factory that helps with creation of new {@link DocumentAttribute} instances as well as translation to concrete
027 * instances from a {@link DocumentAttributeContract}
028 *
029 * @author Kuali Rice Team (rice.collab@kuali.org)
030 */
031public class DocumentAttributeFactory {
032
033    /**
034     * Constructs a document attribute containing character data from the given attribute name and {@link String} value.
035     *
036     * @param name the name of the attribute to construct, must not be a null or blank value
037     * @param value the value of the attribute as a {@code String}
038     *
039     * @return a constructed {@code DocumentAttributeString} representing the document attribute
040     *
041     * @throws IllegalArgumentException if name is a null or blank value
042     */
043    public static DocumentAttributeString createStringAttribute(String name, String value) {
044        DocumentAttributeString.Builder builder = DocumentAttributeString.Builder.create(name);
045        builder.setValue(value);
046        return builder.build();
047    }
048
049    /**
050     * Constructs a document attribute containing date/time data from the given attribute name and {@link DateTime}
051     * object.
052     *
053     * @param name the name of the attribute to construct, must not be a null or blank value
054     * @param value the value of the attribute as a {@code DateTime}
055     *
056     * @return a constructed {@code DocumentAttributeDateTime} representing the document attribute
057     *
058     * @throws IllegalArgumentException if name is a null or blank value
059     */
060    public static DocumentAttributeDateTime createDateTimeAttribute(String name, DateTime value) {
061        DocumentAttributeDateTime.Builder builder = DocumentAttributeDateTime.Builder.create(name);
062        builder.setValue(value);
063        return builder.build();
064    }
065
066    /**
067     * Constructs a document attribute containing date/time data from the given attribute name and {@link Date} object.
068     *
069     * @param name the name of the attribute to construct, must not be a null or blank value
070     * @param value the value of the attribute as a {@code Date}
071     *
072     * @return a constructed {@code DocumentAttributeDateTime} representing the document attribute
073     *
074     * @throws IllegalArgumentException if name is a null or blank value
075     */
076    public static DocumentAttributeDateTime createDateTimeAttribute(String name, Date value) {
077        return createDateTimeAttribute(name, new DateTime(value));
078    }
079
080    /**
081     * Constructs a document attribute containing date/time data from the given attribute name and a numeric long
082     * representing the number of milliseconds from 1970-01-01T00:00:00Z in the default time zone.
083     *
084     * @param name the name of the attribute to construct, must not be a null or blank value
085     * @param instant the instant value represented as milliseconds from 1970-01-01T00:00:00Z
086     *
087     * @return a constructed {@code DocumentAttributeDateTime} representing the document attribute
088     *
089     * @throws IllegalArgumentException if name is a null or blank value
090     */
091    public static DocumentAttributeDateTime createDateTimeAttribute(String name, long instant) {
092        return createDateTimeAttribute(name, new DateTime(instant));
093    }
094
095    /**
096     * Constructs a document attribute containing date/time data from the given attribute name and {@link Calendar}
097     * object.
098     *
099     * @param name the name of the attribute to construct, must not be a null or blank value
100     * @param value the value of the attribute as a {@code Calendar}
101     *
102     * @return a constructed {@code DocumentAttributeDateTime} representing the document attribute
103     *
104     * @throws IllegalArgumentException if name is a null or blank value
105     */
106    public static DocumentAttributeDateTime createDateTimeAttribute(String name, Calendar value) {
107        return createDateTimeAttribute(name, new DateTime(value));
108    }
109
110    /**
111     * Constructs a document attribute containing real number data from the given attribute name and {@link BigDecimal}
112     * object.
113     *
114     * @param name the name of the attribute to construct, must not be a null or blank value
115     * @param value the value of the attribute as a {@code BigDecimal}
116     *
117     * @return a constructed {@code DocumentAttributeDecimal} representing the document attribute
118     *
119     * @throws IllegalArgumentException if name is a null or blank value
120     */
121    public static DocumentAttributeDecimal createDecimalAttribute(String name, BigDecimal value) {
122        DocumentAttributeDecimal.Builder builder = DocumentAttributeDecimal.Builder.create(name);
123        builder.setValue(value);
124        return builder.build();
125    }
126
127    /**
128     * Constructs a document attribute containing real number data from the given attribute name and {@link Number}
129     * object.  The given number is first translated to a {@code BigDecimal} using {@link Number#doubleValue()}.
130     *
131     * @param name the name of the attribute to construct, must not be a null or blank value
132     * @param value the value of the attribute as a {@code Number}
133     *
134     * @return a constructed {@code DocumentAttributeDecimal} representing the document attribute
135     *
136     * @throws IllegalArgumentException if name is a null or blank value
137     */
138    public static DocumentAttributeDecimal createDecimalAttribute(String name, Number value) {
139        return createDecimalAttribute(name, BigDecimal.valueOf(value.doubleValue()));
140    }
141
142    /**
143     * Constructs a document attribute containing integer number data from the given attribute name and
144     * {@link BigInteger} object.
145     *
146     * @param name the name of the attribute to construct, must not be a null or blank value
147     * @param value the value of the attribute as a {@code BigInteger}
148     *
149     * @return a constructed {@code DocumentAttributeInteger} representing the document attribute
150     *
151     * @throws IllegalArgumentException if name is a null or blank value
152     */
153    public static DocumentAttributeInteger createIntegerAttribute(String name, BigInteger value) {
154        DocumentAttributeInteger.Builder builder = DocumentAttributeInteger.Builder.create(name);
155        builder.setValue(value);
156        return builder.build();
157    }
158
159    /**
160     * Constructs a document attribute containing integer number data from the given attribute name and {@link Number}
161     * object.  The given number is first translated to a {@code BigInteger} using {@link Number#longValue()}.
162     *
163     * @param name the name of the attribute to construct, must not be a null or blank value
164     * @param value the value of the attribute as a {@code Number}
165     *
166     * @return a constructed {@code DocumentAttributeInteger} representing the document attribute
167     *
168     * @throws IllegalArgumentException if name is a null or blank value
169     */
170    public static DocumentAttributeInteger createIntegerAttribute(String name, Number value) {
171        return createIntegerAttribute(name, BigInteger.valueOf(value.longValue()));
172    }
173
174    /**
175     * Loads the given {@link DocumentAttributeContract} into the appropriate builder instance based on the type of the
176     * given contract implementation.
177     *
178     * @param contract the contract to load into a builder
179     * @return an implementation of {@link DocumentAttribute.AbstractBuilder} which handles instance of the given
180     * contract
181     *
182     * @throws IllegalArgumentException if the given contract is null
183     * @throws IllegalArgumentException if a builder implementation could not be determined into which to load the given
184     * contract implementation
185     */
186    public static DocumentAttribute.AbstractBuilder<?> loadContractIntoBuilder(DocumentAttributeContract contract) {
187        if (contract == null) {
188            throw new IllegalArgumentException("contract was null");
189        }
190        if (contract instanceof DocumentAttributeString) {
191            DocumentAttributeString attribute = (DocumentAttributeString)contract;
192            DocumentAttributeString.Builder builder = DocumentAttributeString.Builder.create(attribute.getName());
193            builder.setValue(attribute.getValue());
194            return builder;
195        } else if (contract instanceof DocumentAttributeDateTime) {
196            DocumentAttributeDateTime attribute = (DocumentAttributeDateTime)contract;
197            DocumentAttributeDateTime.Builder builder = DocumentAttributeDateTime.Builder.create(attribute.getName());
198            builder.setValue(attribute.getValue());
199            return builder;
200        } else if (contract instanceof DocumentAttributeInteger) {
201            DocumentAttributeInteger attribute = (DocumentAttributeInteger)contract;
202            DocumentAttributeInteger.Builder builder = DocumentAttributeInteger.Builder.create(attribute.getName());
203            builder.setValue(attribute.getValue());
204            return builder;
205        } else if (contract instanceof DocumentAttributeDecimal) {
206            DocumentAttributeDecimal attribute = (DocumentAttributeDecimal)contract;
207            DocumentAttributeDecimal.Builder builder = DocumentAttributeDecimal.Builder.create(attribute.getName());
208            builder.setValue(attribute.getValue());
209            return builder;
210        }
211        throw new IllegalArgumentException("Given document attribute class could not be converted: " + contract.getClass());
212    }
213
214
215}