Java tutorial
/** * MOTECH PLATFORM OPENSOURCE LICENSE AGREEMENT * * Copyright (c) 2010-11 The Trustees of Columbia University in the City of * New York and Grameen Foundation USA. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. Neither the name of Grameen Foundation USA, Columbia University, or * their respective contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY GRAMEEN FOUNDATION USA, COLUMBIA UNIVERSITY * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRAMEEN FOUNDATION * USA, COLUMBIA UNIVERSITY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package org.motechproject.mobile.omi.service; import org.apache.log4j.Logger; import org.motechproject.mobile.core.dao.GatewayRequestDetailsDAO; import org.motechproject.mobile.core.dao.MessageRequestDAO; import org.motechproject.mobile.core.manager.CoreManager; import org.motechproject.mobile.core.model.*; import org.motechproject.mobile.omi.manager.MessageStoreManager; import org.motechproject.mobile.omi.manager.StatusHandler; import org.motechproject.mobile.omp.manager.OMPManager; import org.motechproject.mobile.omp.service.MobileMessagingService; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.Date; /** * @see org.motechproject.mobile.omi.service.OMIServiceWorker * * @author Henry Sampson(henry@dreamoval.com) */ public class OMIServiceWorkerImpl implements OMIServiceWorker, ApplicationContextAware { private static Logger logger = Logger.getLogger(OMIServiceWorkerImpl.class); private MessageStoreManager storeManager; private OMPManager ompManager; private CoreManager coreManager; private StatusHandler statHandler; private int maxTries; private ApplicationContext applicationContext; public OMIServiceWorkerImpl() { } /** * Sends a MessageRequest * * @param messageRequest * @param defaultLanguage * * @see org.motechproject.mobile.omi.service.OMIServiceWorker#processMessageRequest */ @Transactional(propagation = Propagation.REQUIRES_NEW) public void processMessageRequest(MessageRequest messageRequest, Language defaultLanguage) { if (messageRequest != null) { MessageRequestDAO messageRequestDAO = getCoreManager().createMessageRequestDAO(); GatewayRequest gatewayRequest = getStoreManager().constructMessage(messageRequest, defaultLanguage); messageRequest.setGatewayRequestDetails(gatewayRequest.getGatewayRequestDetails()); if (messageRequest.getLanguage() == null) { messageRequest.setLanguage(defaultLanguage); } MobileMessagingService msgSvc = getOmpManager().createMessagingService(); msgSvc.scheduleTransactionalMessage(gatewayRequest); messageRequest.setDateProcessed(new Date()); messageRequest.setStatus(MStatus.PENDING); logger.debug(messageRequest); messageRequestDAO.merge(messageRequest); } } /** * Reschedules a <code>MessageRequest</code> for sending * * The schedule is only added if <code>maxRetries</code> on the <code>MessageRequest</code> has not been reached * * @param message <code>MessageRequest</code> to reschedule * @see org.motechproject.mobile.omi.service.OMIServiceWorker#processMessageRetry */ @Transactional(propagation = Propagation.REQUIRES_NEW) public void processMessageRetry(MessageRequest message) { if (message != null) { MessageRequestDAO msgReqDao = getCoreManager().createMessageRequestDAO(); GatewayRequestDetailsDAO gwReqDao = getCoreManager().createGatewayRequestDetailsDAO(); MobileMessagingService msgSvc = getOmpManager().createMessagingService(); if (message.getTryNumber() >= getMaxTries()) { message.setStatus(MStatus.FAILED); } else { message.setTryNumber(message.getTryNumber() + 1); if (message.getGatewayRequestDetails() == null) { return; } GatewayRequestDetails gwReqDet = (GatewayRequestDetails) gwReqDao .getById(message.getGatewayRequestDetails().getId()); GatewayRequest gwReq = (GatewayRequest) applicationContext.getBean("gatewayRequest", GatewayRequest.class); gwReq.setDateFrom(message.getDateFrom()); gwReq.setDateTo(message.getDateTo()); gwReq.setMessageRequest(message); gwReq.setRecipientsNumber(getStoreManager().formatPhoneNumber(message.getRecipientNumber(), message.getMessageType())); gwReq.setRequestId(message.getRequestId()); gwReq.setTryNumber(message.getTryNumber()); gwReq.setMessage(gwReqDet.getMessage()); gwReq.setMessageStatus(MStatus.SCHEDULED); gwReqDet.getGatewayRequests().add(gwReq); msgSvc.scheduleMessage(gwReqDet); message.setStatus(MStatus.PENDING); } msgReqDao.merge(message); } } /** * Syncs the status of a <code>GatewayRequest</code> to its corresponding <code>MessageRequest</code> * * @param response the <code>Gateway</code> to sync * @see org.motechproject.mobile.omi.service.OMIServiceWorker#processMessageResponse */ @Transactional(propagation = Propagation.REQUIRES_NEW) public void processMessageResponse(GatewayResponse response) { if (response != null) { MessageRequestDAO msgReqDao = getCoreManager().createMessageRequestDAO(); MessageRequest message = response.getGatewayRequest().getMessageRequest(); if (response.getMessageStatus() == MStatus.RETRY && message.getTryNumber() >= getMaxTries()) { response.setMessageStatus(MStatus.FAILED); } getStatHandler().handleStatus(response); message.setStatus(response.getMessageStatus()); msgReqDao.merge(message); } } /** * Merges a <code>MessageRequest</code> to the persistent store.This is needed if a dirty version of the objects exists * within the same persistet context. * * @param mr <code>MessageReques<./code> to merge * @see org.motechproject.mobile.omi.service.OMIServiceWorker#mergeMessageNow */ @Transactional(propagation = Propagation.REQUIRES_NEW) public void mergeMessageNow(MessageRequest mr) { if (mr != null) { MessageRequestDAO msgreqDao = getCoreManager().createMessageRequestDAO(); msgreqDao.merge(mr); } } /** * @return the storeManager */ public MessageStoreManager getStoreManager() { return storeManager; } /** * @param storeManager the storeManager to set */ public void setStoreManager(MessageStoreManager storeManager) { this.storeManager = storeManager; } /** * @return the ompManager */ public OMPManager getOmpManager() { return ompManager; } /** * @param ompManager the ompManager to set */ public void setOmpManager(OMPManager ompManager) { this.ompManager = ompManager; } /** * @return the coreManager */ public CoreManager getCoreManager() { return coreManager; } /** * @param coreManager the coreManager to set */ public void setCoreManager(CoreManager coreManager) { this.coreManager = coreManager; } /** * @return the statHandler */ public StatusHandler getStatHandler() { return statHandler; } /** * @param statHandler the statHandler to set */ public void setStatHandler(StatusHandler statHandler) { this.statHandler = statHandler; } /** * @return the maxTries */ public int getMaxTries() { return maxTries; } /** * @param maxTries the maxTries to set */ public void setMaxTries(int maxTries) { this.maxTries = maxTries; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }