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