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.ksb.messaging;
017
018import org.kuali.rice.core.api.config.CoreConfigHelper;
019import org.kuali.rice.core.api.util.RiceUtilities;
020import org.kuali.rice.krad.data.jpa.PortableSequenceGenerator;
021import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
022import org.kuali.rice.ksb.api.messaging.AsynchronousCall;
023import org.kuali.rice.ksb.service.KSBServiceLocator;
024import org.kuali.rice.ksb.util.KSBConstants;
025
026import javax.persistence.Column;
027import javax.persistence.Entity;
028import javax.persistence.GeneratedValue;
029import javax.persistence.Id;
030import javax.persistence.NamedQueries;
031import javax.persistence.NamedQuery;
032import javax.persistence.Table;
033import javax.persistence.Transient;
034import javax.persistence.Version;
035import java.sql.Timestamp;
036
037/**
038 * A message which has been persisted to the data store.
039 *
040 * @author Kuali Rice Team (rice.collab@kuali.org)
041 */
042@Entity
043@Table(name="KRSB_MSG_QUE_T")
044@NamedQueries({
045  @NamedQuery(name="PersistedMessageBO.FindAll", query="select pm from PersistedMessageBO pm"),
046  @NamedQuery(name="PersistedMessageBO.FindByServiceName", query="select pm from PersistedMessageBO pm where pm.serviceName = :serviceName and pm.methodName = :methodName"),
047  @NamedQuery(name="PersistedMessageBO.GetNextDocuments", query="select pm from PersistedMessageBO pm where pm.applicationId = :applicationId and pm.queueStatus <> :queueStatus and pm.ipNumber = :ipNumber order by pm.queuePriority asc, pm.routeQueueId asc, pm.queueDate asc")
048})
049public class PersistedMessageBO implements PersistedMessage {
050
051        private static final long serialVersionUID = -7047766894738304195L;
052
053        @Id
054        @GeneratedValue(generator = "KRSB_MSG_QUE_S")
055    @PortableSequenceGenerator(name = "KRSB_MSG_QUE_S")
056        @Column(name="MSG_QUE_ID")
057        private Long routeQueueId;
058
059        @Column(name="PRIO")
060        private Integer queuePriority;
061
062        @Column(name="STAT_CD")
063        private String queueStatus;
064
065        @Column(name="DT")
066        private Timestamp queueDate;
067
068        @Column(name="EXP_DT")
069        private Timestamp expirationDate;
070
071        @Column(name="RTRY_CNT")
072        private Integer retryCount;
073
074        @Version
075        @Column(name="VER_NBR")
076        private Integer lockVerNbr;
077
078    @Column(name="IP_NBR")
079        private String ipNumber;
080
081    @Column(name="SVC_NM")
082        private String serviceName;
083
084    @Column(name="APPL_ID")
085        private String applicationId;
086
087    @Column(name="SVC_MTHD_NM")
088        private String methodName;
089
090    @Column(name="APP_VAL_ONE")
091    private String value1;
092
093    @Column(name="APP_VAL_TWO")
094    private String value2;
095
096    @Transient private AsynchronousCall methodCall;
097    @Transient private PersistedMessagePayload payload;
098    
099    public PersistedMessageBO() {
100        // default constructor
101    }
102
103    public static PersistedMessageBO buildMessage(ServiceConfiguration serviceConfiguration, AsynchronousCall methodCall) {
104        PersistedMessageBO message = new PersistedMessageBO();
105        message.setPayload(new PersistedMessagePayload(methodCall, message));
106        message.setIpNumber(RiceUtilities.getIpNumber());
107        message.setServiceName(serviceConfiguration.getServiceName().toString());
108        message.setQueueDate(new Timestamp(System.currentTimeMillis()));
109        if (serviceConfiguration.getPriority() == null) {
110            message.setQueuePriority(KSBConstants.ROUTE_QUEUE_DEFAULT_PRIORITY);
111        } else {
112            message.setQueuePriority(serviceConfiguration.getPriority());
113        }
114        message.setQueueStatus(KSBConstants.ROUTE_QUEUE_QUEUED);
115        message.setRetryCount(0);
116        if (serviceConfiguration.getMillisToLive() > 0) {
117            message.setExpirationDate(new Timestamp(System.currentTimeMillis() + serviceConfiguration.getMillisToLive()));
118        }
119        message.setApplicationId(CoreConfigHelper.getApplicationId());
120        message.setMethodName(methodCall.getMethodName());
121        return message;
122    }
123
124    public PersistedMessageBO copy() {
125        PersistedMessageBO message = new PersistedMessageBO();
126        message.routeQueueId = routeQueueId;
127        message.queuePriority = queuePriority;
128        message.queueStatus = queueStatus;
129        message.queueDate = queueDate;
130        message.expirationDate = expirationDate;
131        message.retryCount = retryCount;
132        message.lockVerNbr = lockVerNbr;
133        message.ipNumber = ipNumber;
134        message.serviceName = serviceName;
135        message.applicationId = applicationId;
136        message.methodName = methodName;
137        message.value1 = value1;
138        message.value2 = value2;
139        message.methodCall = methodCall;
140        message.payload = payload;
141        return message;
142    }
143
144        @Override
145    public String getApplicationId() {
146                return this.applicationId;
147        }
148
149        public void setApplicationId(String applicationId) {
150                this.applicationId = applicationId;
151        }
152
153    @Override
154    public String getIpNumber() {
155        return this.ipNumber;
156    }
157    
158    public void setIpNumber(String ipNumber) {
159        this.ipNumber = ipNumber;
160    }
161    
162        @Override
163    public Timestamp getQueueDate() {
164                return this.queueDate;
165        }
166
167        @Override
168    public Integer getQueuePriority() {
169                return this.queuePriority;
170        }
171
172        @Override
173    public String getQueueStatus() {
174                return this.queueStatus;
175        }
176
177        @Override
178    public Integer getRetryCount() {
179                return this.retryCount;
180        }
181
182
183        public void setQueueDate(Timestamp timestamp) {
184            this.queueDate = timestamp;
185        }
186
187        public void setQueuePriority(Integer integer) {
188            this.queuePriority = integer;
189        }
190
191        public void setQueueStatus(String string) {
192            this.queueStatus = string;
193        }
194
195        public void setRetryCount(Integer integer) {
196            this.retryCount = integer;
197        }
198
199
200    public Integer getLockVerNbr() {
201        return this.lockVerNbr;
202    }
203    
204    public void setLockVerNbr(Integer lockVerNbr) {
205        this.lockVerNbr = lockVerNbr;
206    }
207    
208    @Override
209    public Long getRouteQueueId() {
210        return this.routeQueueId;
211    }
212    
213    public void setRouteQueueId(Long queueSequence) {
214        this.routeQueueId = queueSequence;
215    }
216    
217        @Override
218    public String getServiceName() {
219                return this.serviceName;
220        }
221
222        public void setServiceName(String serviceName) {
223                this.serviceName = serviceName;
224        }
225
226    public String toString() {
227        return "[RouteQueue: " + ", routeQueueId=" + this.routeQueueId + ", ipNumber=" + this.ipNumber 
228                + "applicationId=" + this.applicationId + ", serviceName=" + this.serviceName + ", methodName=" + methodName 
229                + ", queueStatus=" + this.queueStatus + ", queuePriority=" + this.queuePriority
230                + ", queueDate=" + this.queueDate + "]";
231    }
232
233        @Override
234    public AsynchronousCall getMethodCall() {
235                return this.methodCall;
236        }
237
238        public void setMethodCall(AsynchronousCall methodCall) {
239                this.methodCall = methodCall;
240        }
241
242        @Override
243    public String getMethodName() {
244                return this.methodName;
245        }
246
247        public void setMethodName(String methodName) {
248                this.methodName = methodName;
249        }
250
251        @Override
252    public Timestamp getExpirationDate() {
253                return this.expirationDate;
254        }
255
256        public void setExpirationDate(Timestamp expirationDate) {
257                this.expirationDate = expirationDate;
258        }
259
260    @Override
261    public PersistedMessagePayload getPayload() {
262        if (this.payload == null) {
263            if (this.getRouteQueueId() == null) {
264                return null;
265}           this.payload = KSBServiceLocator.getMessageQueueService().findByPersistedMessageByRouteQueueId(this.getRouteQueueId()); 
266        }
267        return this.payload;
268    }
269
270    public void setPayload(PersistedMessagePayload payload) {
271        this.payload = payload;
272    }
273
274    @Override
275    public String getValue1() {
276        return this.value1;
277    }
278
279    public void setValue1(String value1) {
280        this.value1 = value1;
281    }
282
283    @Override
284    public String getValue2() {
285        return this.value2;
286    }
287
288    public void setValue2(String value2) {
289        this.value2 = value2;
290    }
291
292}