001/**
002 * Copyright 2005-2016 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.action;
017
018import org.apache.commons.lang.StringUtils;
019import org.joda.time.DateTime;
020import org.kuali.rice.core.api.CoreConstants;
021import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
022import org.kuali.rice.core.api.mo.ModelBuilder;
023import org.kuali.rice.core.api.util.jaxb.DateTimeAdapter;
024import org.w3c.dom.Element;
025
026import javax.xml.bind.annotation.XmlAccessType;
027import javax.xml.bind.annotation.XmlAccessorType;
028import javax.xml.bind.annotation.XmlAnyElement;
029import javax.xml.bind.annotation.XmlElement;
030import javax.xml.bind.annotation.XmlRootElement;
031import javax.xml.bind.annotation.XmlType;
032import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
033import java.io.Serializable;
034import java.util.Collection;
035
036@XmlRootElement(name = ActionTaken.Constants.ROOT_ELEMENT_NAME)
037@XmlAccessorType(XmlAccessType.NONE)
038@XmlType(name = ActionTaken.Constants.TYPE_NAME, propOrder = {
039    ActionTaken.Elements.ID,
040    ActionTaken.Elements.DOCUMENT_ID,
041    ActionTaken.Elements.PRINCIPAL_ID,
042    ActionTaken.Elements.DELEGATOR_PRINCIPAL_ID,
043    ActionTaken.Elements.DELEGATOR_GROUP_ID,
044    ActionTaken.Elements.ACTION_TAKEN_CODE,
045    ActionTaken.Elements.ACTION_DATE,
046    ActionTaken.Elements.ANNOTATION,
047    ActionTaken.Elements.CURRENT,
048    CoreConstants.CommonElements.FUTURE_ELEMENTS
049})
050public final class ActionTaken extends AbstractDataTransferObject implements ActionTakenContract {
051
052        private static final long serialVersionUID = 8411150332911080837L;
053
054        @XmlElement(name = Elements.ID, required = true)
055    private final String id;
056    
057    @XmlElement(name = Elements.DOCUMENT_ID, required = true)
058    private final String documentId;
059    
060    @XmlElement(name = Elements.PRINCIPAL_ID, required = true)
061    private final String principalId;
062    
063    @XmlElement(name = Elements.DELEGATOR_PRINCIPAL_ID, required = false)
064    private final String delegatorPrincipalId;
065    
066    @XmlElement(name = Elements.DELEGATOR_GROUP_ID, required = false)
067    private final String delegatorGroupId;
068    
069    @XmlElement(name = Elements.ACTION_TAKEN_CODE, required = true)
070    private final String actionTakenCode;
071    
072    @XmlElement(name = Elements.ACTION_DATE, required = true)
073    @XmlJavaTypeAdapter(DateTimeAdapter.class)
074    private final DateTime actionDate;
075
076    @XmlElement(name = Elements.ANNOTATION, required = false)
077    private final String annotation;
078
079    @XmlElement(name = Elements.CURRENT, required = true)
080    private final boolean current;    
081    
082    @SuppressWarnings("unused")
083    @XmlAnyElement
084    private final Collection<Element> _futureElements = null;
085
086    /**
087     * Private constructor used only by JAXB.
088     * 
089     */
090    private ActionTaken() {
091        this.id = null;
092        this.documentId = null;
093        this.principalId = null;
094        this.delegatorPrincipalId = null;
095        this.delegatorGroupId = null;
096        this.actionTakenCode = null;
097        this.actionDate = null;
098        this.annotation = null;
099        this.current = false;
100    }
101
102    private ActionTaken(Builder builder) {
103        this.annotation = builder.getAnnotation();
104        this.id = builder.getId();
105        this.documentId = builder.getDocumentId();
106        this.principalId = builder.getPrincipalId();
107        this.delegatorPrincipalId = builder.getDelegatorPrincipalId();
108        this.delegatorGroupId = builder.getDelegatorGroupId();
109        this.actionTakenCode = builder.getActionTaken().getCode();
110        this.actionDate = builder.getActionDate();
111        this.current = builder.isCurrent();
112    }
113
114    @Override
115    public String getId() {
116        return this.id;
117    }
118
119    @Override
120    public String getDocumentId() {
121        return this.documentId;
122    }
123
124    @Override
125    public String getPrincipalId() {
126        return this.principalId;
127    }
128
129    @Override
130    public String getDelegatorPrincipalId() {
131        return this.delegatorPrincipalId;
132    }
133
134    @Override
135    public String getDelegatorGroupId() {
136        return this.delegatorGroupId;
137    }
138
139    @Override
140    public ActionType getActionTaken() {
141        return ActionType.fromCode(actionTakenCode);
142    }
143
144    @Override
145    public DateTime getActionDate() {
146        return this.actionDate;
147    }
148    
149    @Override
150    public String getAnnotation() {
151        return this.annotation;
152    }
153
154    @Override
155    public boolean isCurrent() {
156        return this.current;
157    }
158
159    /**
160     * A builder which can be used to construct {@link ActionTaken} instances.  Enforces the constraints of the {@link ActionTakenContract}.
161     */
162    public final static class Builder implements Serializable, ModelBuilder, ActionTakenContract {
163
164                private static final long serialVersionUID = -1226075070994810756L;
165
166                private String id;
167        private String documentId;
168        private String principalId;
169        private String delegatorPrincipalId;
170        private String delegatorGroupId;
171        private ActionType actionTaken;
172        private DateTime actionDate;
173        private String annotation;
174        private boolean current;
175
176        private Builder(String id, String documentId, String principalId, ActionType actionTaken) {
177                setId(id);
178                setDocumentId(documentId);
179                setPrincipalId(principalId);
180                setActionTaken(actionTaken);
181            setActionDate(new DateTime());
182                setCurrent(true);
183        }
184
185        public static Builder create(String id, String documentId, String principalId, ActionType actionTaken) {
186            return new Builder(id, documentId, principalId, actionTaken);
187        }
188
189        public static Builder create(ActionTakenContract contract) {
190            if (contract == null) {
191                throw new IllegalArgumentException("contract was null");
192            }
193            Builder builder = create(contract.getId(), contract.getDocumentId(), contract.getPrincipalId(), contract.getActionTaken());
194            builder.setDelegatorPrincipalId(contract.getDelegatorPrincipalId());
195            builder.setDelegatorGroupId(contract.getDelegatorGroupId());
196            builder.setActionDate(contract.getActionDate());
197            builder.setAnnotation(contract.getAnnotation());
198            builder.setCurrent(contract.isCurrent());
199            return builder;
200        }
201
202        public ActionTaken build() {
203            return new ActionTaken(this);
204        }
205
206        @Override
207        public String getId() {
208            return this.id;
209        }
210
211        @Override
212        public String getDocumentId() {
213            return this.documentId;
214        }
215
216        @Override
217        public String getPrincipalId() {
218            return this.principalId;
219        }
220
221        @Override
222        public String getDelegatorPrincipalId() {
223            return this.delegatorPrincipalId;
224        }
225
226        @Override
227        public String getDelegatorGroupId() {
228            return this.delegatorGroupId;
229        }
230
231        @Override
232        public ActionType getActionTaken() {
233            return this.actionTaken;
234        }
235
236        @Override
237        public DateTime getActionDate() {
238            return this.actionDate;
239        }
240        
241        @Override
242        public String getAnnotation() {
243            return this.annotation;
244        }
245
246        @Override
247        public boolean isCurrent() {
248            return this.current;
249        }
250
251        public void setId(String id) {
252            if (StringUtils.isWhitespace(id)) {
253                throw new IllegalArgumentException("id was blank");
254            }
255            this.id = id;
256        }
257
258        public void setDocumentId(String documentId) {
259                if (StringUtils.isBlank(documentId)) {
260                throw new IllegalArgumentException("documentId was null or blank");
261            }
262            this.documentId = documentId;
263        }
264
265        public void setPrincipalId(String principalId) {
266                if (StringUtils.isBlank(principalId)) {
267                throw new IllegalArgumentException("principalId was null or blank");
268            }
269            this.principalId = principalId;
270        }
271
272        public void setDelegatorPrincipalId(String delegatorPrincipalId) {
273            this.delegatorPrincipalId = delegatorPrincipalId;
274        }
275
276        public void setDelegatorGroupId(String delegatorGroupId) {
277            this.delegatorGroupId = delegatorGroupId;
278        }
279
280        public void setActionTaken(ActionType actionTaken) {
281                if (actionTaken == null) {
282                throw new IllegalArgumentException("actionTaken was null");
283            }
284            this.actionTaken = actionTaken;
285        }
286
287        public void setActionDate(DateTime actionDate) {
288            if (actionDate == null) {
289                throw new IllegalArgumentException("actionDate was null");
290            }
291            this.actionDate = actionDate;
292        }
293
294        public void setAnnotation(String annotation) {
295            this.annotation = annotation;
296        }
297        
298        public void setCurrent(boolean current) {
299            this.current = current;
300        }
301
302    }
303
304    /**
305     * Defines some internal constants used on this class.
306     */
307    static class Constants {
308        final static String ROOT_ELEMENT_NAME = "actionTaken";
309        final static String TYPE_NAME = "ActionTakenType";
310    }
311
312
313    /**
314     * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
315     */
316    static class Elements {
317        final static String ID = "id";
318        final static String DOCUMENT_ID = "documentId";
319        final static String PRINCIPAL_ID = "principalId";
320        final static String DELEGATOR_PRINCIPAL_ID = "delegatorPrincipalId";
321        final static String DELEGATOR_GROUP_ID = "delegatorGroupId";
322        final static String ACTION_TAKEN_CODE = "actionTakenCode";
323        final static String ACTION_DATE = "actionDate";
324        final static String ANNOTATION = "annotation";
325        final static String CURRENT = "current";
326    }
327
328}