com.crm.subscriber.service.impl.SubscriberOrderLocalServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.crm.subscriber.service.impl.SubscriberOrderLocalServiceImpl.java

Source

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.crm.subscriber.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.URL;

import javax.mail.internet.InternetAddress;

import com.crm.kernel.service.AppDomainLocalServiceUtil;
import com.crm.merchant.model.MerchantAgent;
import com.crm.merchant.model.MerchantEntry;
import com.crm.merchant.service.MerchantAgentLocalServiceUtil;
import com.crm.merchant.service.MerchantEntryLocalServiceUtil;
import com.crm.product.model.ProductEntry;
import com.crm.product.service.ProductEntryLocalServiceUtil;
import com.crm.subscriber.model.SubscriberOrder;
import com.crm.subscriber.service.base.SubscriberOrderLocalServiceBaseImpl;
import com.crm.util.DisplayUtil;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;
import com.liferay.util.mail.MailEngine;
import com.liferay.util.mail.MailEngineException;

import org.apache.axis.client.Service;

import com.gateway.model.SMSRequest;
import com.gateway.model.ServiceResponse;
import com.gateway.service.ChargingHttpBindingStub;

/**
 * The implementation of the subscriber order local service.
 * 
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link com.crm.subscriber.service.SubscriberOrderLocalService} interface.
 * 
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 * 
 * @author Phan Viet Thang
 * @see com.crm.subscriber.service.base.SubscriberOrderLocalServiceBaseImpl
 * @see com.crm.subscriber.service.SubscriberOrderLocalServiceUtil
 */
public class SubscriberOrderLocalServiceImpl extends SubscriberOrderLocalServiceBaseImpl {
    /*
     * NOTE FOR DEVELOPERS:
     * 
     * Never reference this interface directly. Always use {@link
     * com.crm.subscriber.service.SubscriberOrderLocalServiceUtil} to access the
     * subscriber order local service.
     */
    public SubscriberOrder fetchOrder(long orderId, String isdn, Date orderDate, Date endDate)
            throws SystemException, PortalException {
        return subscriberOrderFinder.fetchOrder(orderId, orderDate, endDate);
    }

    public void emailTicket(long userId, long orderId, Date orderDate, String sendTo, String subject,
            String content) throws PortalException, SystemException {
        User user = userLocalService.getUser(userId);

        SubscriberOrder order = subscriberOrderPersistence.fetchByPrimaryKey(orderId);

        String endPoint = AppDomainLocalServiceUtil.getStringDomain("SERVICE", "resend");
        if (Validator.isNull(endPoint)) {
            endPoint = "http://183.91.14.218:8080/ChargingWS/services/charging";
        }
        System.out.println(endPoint);

        try {
            MerchantEntry merchant = MerchantEntryLocalServiceUtil.fetchMerchant(order.getMerchantId());
            MerchantAgent agent = MerchantAgentLocalServiceUtil.fetchAgent(order.getAgentId());
            ProductEntry product = ProductEntryLocalServiceUtil.fetchProduct(order.getProductId());

            if ((merchant != null) && (product != null)) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");

                SMSRequest req = new SMSRequest();

                req.setIsdn(order.getIsdn());
                req.setCpId(order.getMerchantId());
                req.setAgentId(order.getAgentId());
                req.setDeliveryContent(order.getShippingTo());
                req.setPassword(agent.getPassword());
                req.setUsername(agent.getScreenName());
                req.setProduct(product.getAlias());
                req.setReqDate(dateFormat.format(new Date()));

                ChargingHttpBindingStub stub = new ChargingHttpBindingStub(new URL(endPoint), null);

                ServiceResponse res = stub.sendSMS(req);

                if (res != null) {
                    if (!res.getStatus().equalsIgnoreCase("SV0000")) {
                        throw new PortalException("resend-error:" + res.getStatus());
                    }
                }
            }

        } catch (PortalException e) {
            throw e;
        } catch (Exception e) {
            throw new PortalException(e.getMessage());
        }

        String fromAddress = PrefsPropsUtil.getString(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
        String fromName = PrefsPropsUtil.getString(user.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);

        if (Validator.isNull(fromAddress) || Validator.isNull(fromName)) {
            throw new PortalException("unknow-email-sender");
        }

        sendTo = Validator.isNull(sendTo) ? fromAddress : sendTo;
        subject = Validator.isNull(subject) ? "resend content to subscriber" : subject;

        if (Validator.isNull(content) && (order != null)) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:sss");

            content = "User " + user.getScreenName() + " request resend content to subscriber " + order.getIsdn()
                    + ".\r\n";
            content += "Request date: " + dateFormat.format(new Date()) + "\r\n";
            content += "Order date   : " + dateFormat.format(order.getOrderDate()) + "\r\n";
            content += "Product      : " + DisplayUtil.getProductTitle(order.getProductId()) + "\r\n";
            content += "Content      : " + order.getShippingTo() + "\r\n";
        }

        // build bulk receivers
        try {
            InternetAddress from = new InternetAddress(fromAddress, fromName);

            String[] toAddress = StringUtil.split(sendTo);

            MailMessage message = null;

            if (toAddress.length == 1) {
                InternetAddress to = new InternetAddress(toAddress[0]);

                message = new MailMessage(from, to, subject, content, true);
            } else {
                InternetAddress[] to = new InternetAddress[toAddress.length];

                for (int j = 0; j < toAddress.length; j++) {
                    to[j] = new InternetAddress(toAddress[j]);
                }

                message = new MailMessage(from, subject, content, true);
            }

            message.setHTMLFormat(true);
            MailEngine.send(message);
        } catch (MailEngineException e) {
            throw new PortalException(e);
        } catch (Exception e) {
            throw new SystemException(e);
        }
    }

    private static Log log = LogFactoryUtil.getLog(SubscriberOrderLocalServiceImpl.class);
}