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.kcb.bo;
017
018import javax.persistence.CascadeType;
019import javax.persistence.Column;
020import javax.persistence.Entity;
021import javax.persistence.FetchType;
022import javax.persistence.Id;
023import javax.persistence.JoinColumn;
024import javax.persistence.OneToOne;
025import javax.persistence.Table;
026import javax.persistence.Version;
027
028import org.apache.commons.lang.builder.ToStringBuilder;
029
030/**
031 * This class represents an instance of a MessageDelivery.  A Message gets delivered to 
032 * recipients, possibly in various ways.  For each delivery type that a recipient gets sent to them, 
033 * they have an instance of this entity.
034 * @author Kuali Rice Team (rice.collab@kuali.org)
035 */
036@Entity
037@Table(name="KREN_MSG_DELIV_T")
038public class MessageDelivery extends BaseLockable {
039    private static final Integer ZERO = Integer.valueOf(0);
040
041    /**
042     * Field names
043     */
044    public static final String ID_FIELD = "id";
045    public static final String SYSTEMID_FIELD = "delivererSystemId";
046    public static final String MESSAGEID_FIELD = "message";
047    public static final String DELIVERY_STATUS = "deliveryStatus";
048    public static final String PROCESS_COUNT = "processCount";
049    
050    @Id
051        @Column(name="MSG_DELIV_ID")
052        private Long id;
053    @Column(name="TYP_NM", nullable=false)
054        private String delivererTypeName;
055    @Column(name="SYS_ID", nullable=true)
056        private String delivererSystemId;  // can hold an identifier from the endpoint deliverer mechanism system (i.e. workflow id, SMS id, etc)
057    @Column(name="STAT_CD", nullable=true)
058    private String deliveryStatus = MessageDeliveryStatus.UNDELIVERED.name();
059    @Column(name="PROC_CNT", nullable=true)
060    private Integer processCount = ZERO;
061
062    /**
063     * This delivery's message
064     */
065    @OneToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST})
066        @JoinColumn(name="MSG_ID")
067        private Message message;
068
069    /**
070     * Lock column for OJB optimistic locking
071     */
072    @Version
073        @Column(name="VER_NBR")
074        private Integer lockVerNbr;
075    
076    /**
077     * Constructs a MessageDelivery instance.
078     */
079    public MessageDelivery() {
080    }
081
082    /**
083     * Shallow-copy constructor
084     * @param md MessageDelivery to (shallow) copy
085     */
086    public MessageDelivery(MessageDelivery md) {
087        this.id = md.id;
088        this.delivererTypeName = md.delivererTypeName;
089        this.deliveryStatus = md.deliveryStatus;
090        this.delivererSystemId = md.delivererSystemId;
091        this.message = md.message;
092        this.lockVerNbr = md.lockVerNbr;
093    }
094
095    /**
096     * Gets the id attribute. 
097     * @return Returns the id.
098     */
099    public Long getId() {
100        return id;
101    }
102
103    /**
104     * Sets the id attribute value.
105     * @param id The id to set.
106     */
107    public void setId(Long id) {
108        this.id = id;
109    }
110
111
112    /**
113     * Return value of lock column for OJB optimistic locking
114     * @return value of lock column for OJB optimistic locking
115     */
116    public Integer getLockVerNbr() {
117        return lockVerNbr;
118    }
119
120    /**
121     * Set value of lock column for OJB optimistic locking
122     * @param lockVerNbr value of lock column for OJB optimistic locking
123     */
124    public void setLockVerNbr(Integer lockVerNbr) {
125        this.lockVerNbr = lockVerNbr;
126    }
127
128    /**
129     * Gets the deliveryStatus attribute. 
130     * @return Returns the deliveryStatus.
131     */
132    public String getDeliveryStatus() {
133        return deliveryStatus;
134    }
135
136    /**
137     * Convenience method that sets the delivery status in a typesafe manner.
138     * This method is preferred to {@link #setDeliveryStatus(String)}
139     * @param deliveryStatus the MessageDeliveryStatus enum constant
140     */
141    public void setDeliveryStatus(MessageDeliveryStatus deliveryStatus) {
142        this.deliveryStatus = deliveryStatus.name();
143    }
144
145    /**
146     * Sets the deliveryStatus attribute value.
147     * @param deliveryStatus The deliveryStatus to set.
148     */
149    public void setDeliveryStatus(String deliveryStatus) {
150        // Enums will throw an IllegalArgumentException from valueOf if there
151        // is no matching enum
152        MessageDeliveryStatus.valueOf(deliveryStatus);
153        this.deliveryStatus = deliveryStatus;
154    }
155
156    /**
157     * @return the number of times processing has been attempted for this message
158     */
159    public Integer getProcessCount() {
160        return this.processCount;
161    }
162
163    /**
164     * Sets the number of times processing has been attempted for this message
165     * @param processCount the number of times processing has been attempted for this message
166     */
167    public void setProcessCount(Integer processCount) {
168        this.processCount = processCount;
169    }
170
171    /**
172     * Gets the delivererTypeName attribute. 
173     * @return Returns the delivererTypeName.
174     */
175    public String getDelivererTypeName() {
176        return delivererTypeName;
177    }
178
179    /**
180     * Sets the delivererTypeName attribute value.
181     * @param delivererTypeName The delivererTypeName to set.
182     */
183    public void setDelivererTypeName(String delivererTypeName) {
184        this.delivererTypeName = delivererTypeName;
185    }
186
187    /**
188     * Gets the delivererSystemId attribute. 
189     * @return Returns the delivererSystemId.
190     */
191    public String getDelivererSystemId() {
192        return delivererSystemId;
193    }
194
195    /**
196     * Sets the delivererSystemId attribute value.
197     * @param delivererSystemId The delivererSystemId to set.
198     */
199    public void setDelivererSystemId(String delivererSystemId) {
200        this.delivererSystemId = delivererSystemId;
201    }
202
203    /**
204     * @return this delivery's message
205     */
206    public Message getMessage() {
207        return this.message;
208    }
209
210    /**
211     * Sets this delivery's message
212     * @param message the message to set
213     */
214    public void setMessage(Message message) {
215        this.message = message;
216    }
217
218    /**
219     * @see java.lang.Object#toString()
220     */
221    @Override
222    public String toString() {
223        return new ToStringBuilder(this)
224                       .append("id", id)
225                       .append("deliveryStatus", deliveryStatus)
226                       .append("processCount", processCount)
227                       .append("delivererTypename", delivererTypeName)
228                       .append("delivererSystemId", delivererSystemId)
229                       .append("message", message == null ? null : message.getId())
230                       .toString();
231    }
232}