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.kim.api.role;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.CoreConstants;
020import org.kuali.rice.core.api.mo.AbstractDataTransferObject;
021import org.kuali.rice.core.api.mo.ModelBuilder;
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@XmlRootElement(name = RolePermission.Constants.ROOT_ELEMENT_NAME)
035@XmlAccessorType(XmlAccessType.NONE)
036@XmlType(name = RolePermission.Constants.TYPE_NAME, propOrder = {
037        RolePermission.Elements.ID,
038        RolePermission.Elements.ROLE_ID,
039        RolePermission.Elements.PERMISSION_ID,
040        RolePermission.Elements.ACTIVE,
041        CoreConstants.CommonElements.VERSION_NUMBER,
042        CoreConstants.CommonElements.OBJECT_ID,
043        CoreConstants.CommonElements.FUTURE_ELEMENTS
044})
045public class RolePermission extends AbstractDataTransferObject implements RolePermissionContract {
046    @XmlElement(name = Elements.ID, required = false)
047    private final String id;
048
049    @XmlElement(name = RolePermission.Elements.ROLE_ID, required = false)
050    private final String roleId;
051
052    @XmlElement(name = RolePermission.Elements.PERMISSION_ID, required = false)
053    private final String permissionId;
054
055    @XmlElement(name = RolePermission.Elements.ACTIVE, required = false)
056    private final boolean active;
057
058    @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
059    private final Long versionNumber;
060
061    @XmlElement(name = CoreConstants.CommonElements.OBJECT_ID, required = false)
062    private final String objectId;
063
064    @SuppressWarnings("unused")
065    @XmlAnyElement
066    private final Collection<Element> _futureElements = null;
067
068    /**
069     * A constructor to be used only by JAXB unmarshalling.
070     */
071    private RolePermission() {
072        this.id = null;
073        this.roleId = null;
074        this.permissionId = null;
075        this.active = false;
076        this.versionNumber = null;
077        this.objectId = null;
078    }
079
080    /**
081     * A constructor using the Builder.
082     *
083     * @param builder
084     */
085    public RolePermission(Builder builder) {
086        this.id = builder.getId();
087        this.roleId = builder.getRoleId();
088        this.permissionId = builder.getPermissionId();
089        this.active = builder.isActive();
090        this.versionNumber = builder.getVersionNumber();
091        this.objectId = builder.getObjectId();
092    }
093
094    @Override
095    public String getId() {
096        return id;
097    }
098
099    @Override
100    public String getPermissionId() {
101        return permissionId;
102    }
103
104    @Override
105    public String getRoleId() {
106        return roleId;
107    }
108
109    @Override
110    public boolean isActive() {
111        return active;
112    }
113
114    @Override
115    public Long getVersionNumber() {
116        return versionNumber;
117    }
118
119    @Override
120    public String getObjectId() {
121        return objectId;
122    }
123
124    /**
125     * This builder constructs a RolePermission enforcing the constraints of the {@link RolePermissionContract}.
126     */
127    public static final class Builder implements RolePermissionContract, ModelBuilder, Serializable {
128        private String id;
129        private String roleId;
130        private String permissionId;
131        private Long versionNumber = 1L;
132        private String objectId;
133        private boolean active;
134
135        private Builder(String id, String roleId, String permissionId) {
136            setId(id);
137            setRoleId(roleId);
138            setPermissionId(permissionId);
139        }
140
141        /**
142         * Creates a RolePermission with the required fields.
143         */
144        public static Builder create(String id, String roleId, String permissionId) {
145            return new Builder(id, roleId, permissionId);
146        }
147
148        /**
149         * Creates a RolePermission from an existing {@link RolePermissionContract}.
150         */
151        public static Builder create(RolePermissionContract contract) {
152            Builder builder = new Builder(contract.getId(), contract.getRoleId(), contract.getPermissionId());
153            builder.setActive(contract.isActive());
154            builder.setVersionNumber(contract.getVersionNumber());
155            builder.setObjectId(contract.getObjectId());
156
157            return builder;
158        }
159
160        @Override
161        public String getId() {
162            return id;
163        }
164
165        public void setId(final String id) {
166            if (StringUtils.isWhitespace(id)) {
167                throw new IllegalArgumentException("id is blank");
168            }
169            this.id = id;
170        }
171
172        @Override
173        public String getPermissionId() {
174            return permissionId;
175        }
176
177        public void setPermissionId(final String permissionId) {
178            this.permissionId = permissionId;
179        }
180
181        @Override
182        public String getRoleId() {
183            return roleId;
184        }
185
186        public void setRoleId(final String roleId) {
187            this.roleId = roleId;
188        }
189
190        @Override
191        public Long getVersionNumber() {
192            return versionNumber;
193        }
194
195        public void setVersionNumber(Long versionNumber) {
196            if (versionNumber != null && versionNumber <= 0) {
197                throw new IllegalArgumentException("versionNumber is invalid");
198            }
199            this.versionNumber = versionNumber;
200        }
201
202        @Override
203        public String getObjectId() {
204            return objectId;
205        }
206
207        public void setObjectId(final String objectId) {
208            this.objectId = objectId;
209        }
210
211        @Override
212        public boolean isActive() {
213            return active;
214        }
215
216        public void setActive(final boolean active) {
217            this.active = active;
218        }
219
220        @Override
221        public RolePermission build() {
222            if (versionNumber == null || versionNumber <= 0) {
223                throw new IllegalStateException("versionNumber is invalid");
224            }
225            if (StringUtils.isWhitespace(id)) {
226                throw new IllegalStateException("id is blank");
227            }
228            return new RolePermission(this);
229        }
230    }
231
232    /**
233     * Defines some internal constants used on this class.
234     */
235    static class Constants {
236        final static String ROOT_ELEMENT_NAME = "rolePermission";
237        final static String TYPE_NAME = "RolePermissionType";
238    }
239
240    /**
241     * A private class which exposes constants which define the XML element names to use
242     * when this object is marshalled to XML.
243     */
244    static class Elements {
245        final static String ID = "id";
246        final static String PERMISSION_ID = "permissionId";
247        final static String ROLE_ID = "roleId";
248        final static String ACTIVE = "active";
249    }
250}