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.document;
017
018import java.io.Serializable;
019import java.util.Collection;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAnyElement;
024import javax.xml.bind.annotation.XmlElement;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlType;
027
028import org.apache.commons.lang.StringUtils;
029import org.kuali.rice.core.api.CoreConstants;
030import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
031import org.kuali.rice.core.api.mo.ModelBuilder;
032import org.w3c.dom.Element;
033
034@XmlRootElement(name = DocumentLink.Constants.ROOT_ELEMENT_NAME)
035@XmlAccessorType(XmlAccessType.NONE)
036@XmlType(name = DocumentLink.Constants.TYPE_NAME, propOrder = {
037            DocumentLink.Elements.ID,
038                DocumentLink.Elements.ORIGINATING_DOCUMENT_ID,
039                DocumentLink.Elements.DESTINATION_DOCUMENT_ID,
040                CoreConstants.CommonElements.FUTURE_ELEMENTS
041})
042public final class DocumentLink extends AbstractDataTransferObject implements DocumentLinkContract {
043
044        private static final long serialVersionUID = -1193048221115914280L;
045
046        @XmlElement(name = Elements.ID, required = false)
047    private final String id;
048
049    @XmlElement(name = Elements.ORIGINATING_DOCUMENT_ID, required = true)
050    private final String originatingDocumentId;
051    
052    @XmlElement(name = Elements.DESTINATION_DOCUMENT_ID, required = true)
053    private final String destinationDocumentId;
054        
055    @SuppressWarnings("unused")
056    @XmlAnyElement
057    private final Collection<Element> _futureElements = null;
058
059    /**
060     * Private constructor used only by JAXB.
061     */
062    private DocumentLink() {
063        this.id = null;
064        this.originatingDocumentId = null;
065        this.destinationDocumentId = null;
066    }
067
068    private DocumentLink(Builder builder) {
069        this.id = builder.getId();
070        this.originatingDocumentId = builder.getOriginatingDocumentId();
071        this.destinationDocumentId = builder.getDestinationDocumentId();
072    }
073
074    @Override
075    public String getId() {
076        return this.id;
077    }
078    
079    @Override
080    public String getOriginatingDocumentId() {
081        return this.originatingDocumentId;
082    }
083
084    @Override
085    public String getDestinationDocumentId() {
086        return this.destinationDocumentId;
087    }
088
089    /**
090     * A builder which can be used to construct {@link DocumentLink} instances.  Enforces the constraints of the {@link DocumentLinkContract}.
091     */
092    public final static class Builder implements Serializable, ModelBuilder, DocumentLinkContract {
093
094                private static final long serialVersionUID = -6713990840543140054L;
095
096                private String id;
097        private String originatingDocumentId;
098        private String destinationDocumentId;
099
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}