Java tutorial
/** * Copyright (c) 2000-2011 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.product.service.impl; import java.util.Date; import java.util.List; import com.liferay.portal.kernel.exception.PortalException; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Validator; import com.liferay.portal.kernel.workflow.WorkflowConstants; import com.liferay.portal.model.User; import com.crm.util.ValidateUtil; import com.crm.product.model.ProductConfig; import com.crm.product.model.ProductEntry; import com.crm.product.service.base.ProductEntryLocalServiceBaseImpl; /** * The implementation of the product entry 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.product.service.ProductEntryLocalService} interface. * </p> * * <p> * Never reference this interface directly. Always use * {@link com.crm.product.service.ProductEntryLocalServiceUtil} to access the * product entry local service. * </p> * * <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 ThangPV * @see com.crm.product.service.base.ProductEntryLocalServiceBaseImpl * @see com.crm.product.service.ProductEntryLocalServiceUtil */ public class ProductEntryLocalServiceImpl extends ProductEntryLocalServiceBaseImpl { public ProductEntry fetchProduct(long productId) throws PortalException, SystemException { return productEntryPersistence.fetchByPrimaryKey(productId); } public ProductEntry getProduct(long productId) throws PortalException, SystemException { return productEntryPersistence.findByPrimaryKey(productId); } public ProductEntry fetchProduct(String code) throws PortalException, SystemException { return productEntryPersistence.fetchByCode(code); } public ProductEntry getProduct(String code) throws PortalException, SystemException { return productEntryPersistence.findByCode(code); } public int countByType(String productType) throws PortalException, SystemException { if (!productType.equals(StringPool.BLANK)) { return productEntryPersistence.countByType(productType); } else { return productEntryPersistence.countAll(); } } public List<ProductEntry> getByType(String productType) throws PortalException, SystemException { if (Validator.isNotNull(productType)) { return productEntryPersistence.findByType(productType); } else { return productEntryPersistence.findAll(); } } protected ProductEntry updateProduct(long userId, ProductEntry product, int status) throws PortalException, SystemException { // Finder User user = userPersistence.findByPrimaryKey(userId); Date now = new Date(); product.setUserId(user.getUserId()); product.setUserName(user.getScreenName()); if (product.isNew()) { product.setCreateDate(now); } product.setModifiedDate(now); status = (status == WorkflowConstants.STATUS_ANY) ? product.getStatus() : status; product.setStatus(status); return productEntryPersistence.update(product, false); } public ProductEntry addProduct(long userId, long categoryId, long merchantId, String productType, String code, String title, String sku, String availBalances, double price, String subscriberTypes, long bccMerchantId, String bccMerchantURL, boolean termOfUse, String termUnit, int termPeriod, boolean subscription, String subscriptionUnit, int subscriptionPeriod, String graceUnit, int gracePeriod, ProductConfig config, int status, String description) throws PortalException, SystemException { validate(code, title, productType, sku, price); ProductEntry product = productEntryPersistence.create(0); product.setAlias(code); product.setTitle(title); product.setProductType(productType); product.setCategoryId(categoryId); product.setMerchantId(merchantId); product.setSku(sku); product.setAvailBalances(availBalances); product.setPrice(price); product.setSubscriberTypes(subscriberTypes); product.setBccMerchantId(bccMerchantId); product.setBccMerchantURL(bccMerchantURL); product.setTermOfUse(termOfUse); product.setTermUnit(termUnit); product.setTermPeriod(termPeriod); product.setSubscription(subscription); product.setSubscriptionUnit(subscriptionUnit); product.setSubscriptionPeriod(subscriptionPeriod); product.setGraceUnit(graceUnit); product.setGracePeriod(gracePeriod); product.setDescription(description); product = updateProduct(userId, product, status); if (Validator.isNotNull(config)) { Date now = new Date(); User user = userLocalService.getUser(userId); config.setUserId(userId); config.setUserName(user.getScreenName()); config.setCreateDate(now); config.setModifiedDate(now); config.setProductId(product.getProductId()); config = productConfigPersistence.update(config, false); } return product; } public ProductEntry updateProduct(long userId, long productId, long categoryId, long merchantId, String productType, String code, String title, String sku, String availBalances, double price, String subscriberTypes, long bccMerchantId, String bccMerchantURL, boolean termOfUse, String termUnit, int termPeriod, boolean subscription, String subscriptionUnit, int subscriptionPeriod, String graceUnit, int gracePeriod, ProductConfig config, int status, String description) throws PortalException, SystemException { validate(code, title, productType, sku, price); ProductEntry product = getProduct(productId); product.setAlias(code); product.setTitle(title); product.setProductType(productType); product.setCategoryId(categoryId); product.setMerchantId(merchantId); product.setSku(sku); product.setAvailBalances(availBalances); product.setPrice(price); product.setSubscriberTypes(subscriberTypes); product.setBccMerchantId(bccMerchantId); product.setBccMerchantURL(bccMerchantURL); product.setTermOfUse(termOfUse); product.setTermUnit(termUnit); product.setTermPeriod(termPeriod); product.setSubscription(subscription); product.setSubscriptionUnit(subscriptionUnit); product.setSubscriptionPeriod(subscriptionPeriod); product.setGraceUnit(graceUnit); product.setGracePeriod(gracePeriod); product.setDescription(description); product = updateProduct(userId, product, status); if (Validator.isNotNull(config)) { try { productConfigPersistence.remove(productId); } catch (Exception e) { } Date now = new Date(); User user = userLocalService.getUser(userId); config.setUserId(userId); config.setUserName(user.getScreenName()); config.setCreateDate(now); config.setModifiedDate(now); config.setProductId(product.getProductId()); config = productConfigPersistence.update(config, false); } return product; } public ProductEntry deleteProduct(long productId) throws PortalException, SystemException { try { productRouteLocalService.deleteByProduct(productId); productActionLocalService.deleteByProduct(productId); productMessageLocalService.deleteByProduct(productId); productPricePersistence.removeByProduct(productId); return productEntryPersistence.remove(productId); } catch (Exception e) { throw new PortalException(e.getMessage()); } } public void addProductResources(long userId, long productId, boolean addGroupPermissions, boolean addGuestPermissions) throws PortalException, SystemException { ProductEntry product = getProduct(productId); addProductResources(userId, product, addGroupPermissions, addGuestPermissions); } public void addProductResources(long userId, long productId, String[] groupPermissions, String[] guestPermissions) throws PortalException, SystemException { addProductResources(userId, getProduct(productId), groupPermissions, guestPermissions); } public void addProductResources(long userId, ProductEntry product, boolean addGroupPermissions, boolean addGuestPermissions) throws PortalException, SystemException { User user = userLocalService.getUser(userId); resourceLocalService.addResources(user.getCompanyId(), user.getGroupId(), product.getUserId(), ProductEntry.class.getName(), product.getProductId(), false, addGroupPermissions, addGuestPermissions); } public void addProductResources(long userId, ProductEntry product, String[] groupPermissions, String[] guestPermissions) throws PortalException, SystemException { User user = userLocalService.getUser(userId); resourceLocalService.addModelResources(user.getCompanyId(), user.getGroupId(), product.getUserId(), ProductEntry.class.getName(), product.getProductId(), groupPermissions, guestPermissions); } protected void validate(String code, String title, String productType, String sku, double price) throws PortalException, SystemException { ValidateUtil.check(productType, code, title); if (price < 0) { throw new PortalException("price-is-positive"); } } }