View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.api.document;
17  
18  import java.io.Serializable;
19  import java.util.Collection;
20  
21  import javax.xml.bind.annotation.XmlAccessType;
22  import javax.xml.bind.annotation.XmlAccessorType;
23  import javax.xml.bind.annotation.XmlAnyElement;
24  import javax.xml.bind.annotation.XmlElement;
25  import javax.xml.bind.annotation.XmlRootElement;
26  import javax.xml.bind.annotation.XmlType;
27  
28  import org.apache.commons.lang.StringUtils;
29  import org.kuali.rice.core.api.CoreConstants;
30  import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
31  import org.kuali.rice.core.api.mo.ModelBuilder;
32  import org.w3c.dom.Element;
33  
34  @XmlRootElement(name = DocumentLink.Constants.ROOT_ELEMENT_NAME)
35  @XmlAccessorType(XmlAccessType.NONE)
36  @XmlType(name = DocumentLink.Constants.TYPE_NAME, propOrder = {
37  	    DocumentLink.Elements.ID,
38  		DocumentLink.Elements.ORIGINATING_DOCUMENT_ID,
39  		DocumentLink.Elements.DESTINATION_DOCUMENT_ID,
40  		CoreConstants.CommonElements.FUTURE_ELEMENTS
41  })
42  public final class DocumentLink extends AbstractDataTransferObject implements DocumentLinkContract {
43  
44  	private static final long serialVersionUID = -1193048221115914280L;
45  
46  	@XmlElement(name = Elements.ID, required = false)
47      private final String id;
48  
49      @XmlElement(name = Elements.ORIGINATING_DOCUMENT_ID, required = true)
50      private final String originatingDocumentId;
51      
52      @XmlElement(name = Elements.DESTINATION_DOCUMENT_ID, required = true)
53      private final String destinationDocumentId;
54          
55      @SuppressWarnings("unused")
56      @XmlAnyElement
57      private final Collection<Element> _futureElements = null;
58  
59      /**
60       * Private constructor used only by JAXB.
61       */
62      private DocumentLink() {
63          this.id = null;
64      	this.originatingDocumentId = null;
65          this.destinationDocumentId = null;
66      }
67  
68      private DocumentLink(Builder builder) {
69          this.id = builder.getId();
70      	this.originatingDocumentId = builder.getOriginatingDocumentId();
71          this.destinationDocumentId = builder.getDestinationDocumentId();
72      }
73  
74      @Override
75      public String getId() {
76          return this.id;
77      }
78      
79      @Override
80      public String getOriginatingDocumentId() {
81          return this.originatingDocumentId;
82      }
83  
84      @Override
85      public String getDestinationDocumentId() {
86          return this.destinationDocumentId;
87      }
88  
89      /**
90       * A builder which can be used to construct {@link DocumentLink} instances.  Enforces the constraints of the {@link DocumentLinkContract}.
91       */
92      public final static class Builder implements Serializable, ModelBuilder, DocumentLinkContract {
93  
94  		private static final long serialVersionUID = -6713990840543140054L;
95  
96  		private String id;
97          private String originatingDocumentId;
98          private String destinationDocumentId;
99  
100         private Builder(String originatingDocumentId, String destinationDocumentId) {
101             setOriginatingDocumentId(originatingDocumentId);
102             setDestinationDocumentId(destinationDocumentId);
103             if (getOriginatingDocumentId().equals(getDestinationDocumentId())) {
104             	throw new IllegalArgumentException("originating and destination document ids were the same, cannot link a document with itself");
105             }
106         }
107 
108         public static Builder create(String originatingDocumentId, String destinationDocumentId) {
109             return new Builder(originatingDocumentId, destinationDocumentId);
110         }
111 
112         public static Builder create(DocumentLinkContract contract) {
113             if (contract == null) {
114                 throw new IllegalArgumentException("contract was null");
115             }
116             Builder builder = create(contract.getOriginatingDocumentId(), contract.getDestinationDocumentId());
117             builder.setId(contract.getId());
118             return builder;
119         }
120 
121         public DocumentLink build() {
122             return new DocumentLink(this);
123         }
124 
125         @Override
126         public String getId() {
127             return this.id;
128         }
129         
130         @Override
131         public String getOriginatingDocumentId() {
132             return this.originatingDocumentId;
133         }
134 
135         @Override
136         public String getDestinationDocumentId() {
137             return this.destinationDocumentId;
138         }
139 
140         public void setId(String id) {
141             this.id = id;
142         }
143 
144         public void setOriginatingDocumentId(String originatingDocumentId) {
145             if (StringUtils.isBlank(originatingDocumentId)) {
146             	throw new IllegalArgumentException("originatingDocumentId was null or blank");
147             }
148             this.originatingDocumentId = originatingDocumentId;
149         }
150 
151         public void setDestinationDocumentId(String destinationDocumentId) {
152         	if (StringUtils.isBlank(destinationDocumentId)) {
153             	throw new IllegalArgumentException("destinationDocumentId was null or blank");
154             }
155             this.destinationDocumentId = destinationDocumentId;
156         }
157 
158     }
159 
160     /**
161      * Defines some internal constants used on this class.
162      */
163     static class Constants {
164         final static String ROOT_ELEMENT_NAME = "documentLink";
165         final static String TYPE_NAME = "DocumentLinkType";
166     }
167 
168     /**
169      * A private class which exposes constants which define the XML element names to use when this object is marshalled to XML.
170      */
171     static class Elements {
172         final static String ID = "id";
173         final static String ORIGINATING_DOCUMENT_ID = "originatingDocumentId";
174         final static String DESTINATION_DOCUMENT_ID = "destinationDocumentId";
175     }
176 
177 }