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