You are on page 1of 6

package com.nomorecards.

services;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Stateless;

import org.apache.log4j.Logger;

import com.nomorecards.exceptions.NMCServicesException;
import com.nomorecards.model.beans.RatingAndReview;
import com.nomorecards.model.beans.User;
import com.nomorecards.model.filters.RatingAndReviewListFilter;
import com.nomorecards.services.interfaces.RatingAndReviewServicesInterface;
import com.nomorecards.services.interfaces.ServicesSession;
import com.nomorecards.services.interfaces.TransportServicesInterface;
import com.nomorecards.services.vo.PageIndexInfo;
import com.nomorecards.services.vo.PageList;
import com.nomorecards.transport.constants.OperationEnum;
import com.nomorecards.transport.constants.TycoonEnum;
import com.nomorecards.transport.exceptions.ExporterException;
import com.nomorecards.transport.exceptions.ImporterException;
import com.nomorecards.transport.exceptions.TransportException;
import com.nomorecards.transport.factories.ObjectCreator;
import com.nomorecards.transport.factories.OperationDataCreator;
import com.nomorecards.transport.factories.OperationDataCreator.ImporterType;
import com.nomorecards.transport.vo.OperationData;

// Import sentences

/*
*
* @(#) .java 1.50 04/04/14
*
* Copyright ©2013 NoMoreCards Inc. All Rights Reserved
*/

/**
* This class instantiates implementations of NMCClass which for the business is...
*
* @author David Mantilla
* @date 7/05/2014
* @version 1.0
* @since 1.7
*/

@Stateless
@Lock(LockType.READ)
public class RatingAndReviewServices implements
RatingAndReviewServicesInterface {

/* ------------------------------------------------------------
* Final attributes
* ------------------------------------------------------------ */
private static final Logger LOG = Logger
.getLogger(RatingAndReviewServices.class);
/* ------------------------------------------------------------
* Static class variables
* ------------------------------------------------------------ */

/* ------------------------------------------------------------
* Instance attributes
* ------------------------------------------------------------ */
@EJB
private TransportServicesInterface transportServices;

/* ------------------------------------------------------------
* Constructors
* ------------------------------------------------------------ */

/* ------------------------------------------------------------
* Getters & setters
* ------------------------------------------------------------ */

/* ------------------------------------------------------------
* Methods
* ------------------------------------------------------------ */

/* (non-Javadoc)
*
* @see
*
com.nomorecards.services.interfaces.RatingAndReviewServicesInterface#getRatingAndRe
views(com.nomorecards.model
* .beans.User, com.nomorecards.model.beans.Item,
*
com.nomorecards.services.interfaces.RatingAndReviewServicesInterface.GetRatingAndRe
viewMode,
* com.nomorecards.model.filters.RatingAndReviewListFilter,
com.nomorecards.services.vo.PageIndexInfo,
* com.nomorecards.services.interfaces.ServicesSession) */
@Override
public PageList<RatingAndReview> getUserRatingAndReviews(User user,
GetRatingAndReviewMode mode, RatingAndReviewListFilter filter,
PageIndexInfo pageIndexInfo, ServicesSession servicesSession)
throws NMCServicesException, TransportException {
LOG.debug("method: getUserRatingAndReviews");
PageList<RatingAndReview> result = null;
try {
OperationData getRatingAndReviewsOp;
if (filter != null) {
getRatingAndReviewsOp =
OperationDataCreator.createOperationData(filter,
null,
servicesSession,
OperationEnum.GET_USER_RATINGS);
} else {
getRatingAndReviewsOp =

OperationDataCreator.createOperationData(servicesSession,
OperationEnum.GET_USER_RATINGS);
}
getRatingAndReviewsOp.setExpectAllFields(true);
getRatingAndReviewsOp.addOrderedField(TycoonEnum.DATE_START,
false);

if (pageIndexInfo != null) {
getRatingAndReviewsOp.setStartLimit(pageIndexInfo
.getStarItemIndex());
getRatingAndReviewsOp.setEndLimit(pageIndexInfo
.getEndItemIndex());
} else {
getRatingAndReviewsOp.setStartLimit(0);
getRatingAndReviewsOp.setEndLimit(Long.MAX_VALUE);
}

switch (mode) {
case AS_SENDER:
getRatingAndReviewsOp.addFilter(TycoonEnum.NMC_ID,
user
.getId());
break;
case AS_ASSIGNED_TO:
getRatingAndReviewsOp.addFilter(TycoonEnum.NMC_ID,
user
.getId());
break;
default:
throw new UnsupportedOperationException();
}

transportServices.sendOperation(getRatingAndReviewsOp, true);
@SuppressWarnings("unchecked")
List<RatingAndReview> ratings =
ObjectCreator.createBeanList(RatingAndReview.class,
getRatingAndReviewsOp, ArrayList.class);
result =
new PageList<RatingAndReview>(ratings,
getRatingAndReviewsOp);
LOG.debug("success: getUserRatingAndReviews");

} catch (ExporterException | ImporterException e) {


throw new RuntimeException(" Runtime exception ", e);
}

return result;
}

/* (non-Javadoc)
*
* @see
*
com.nomorecards.services.interfaces.RatingAndReviewServicesInterface#reportToAdmin(
com.nomorecards.model.beans
* .RatingAndReview, com.nomorecards.services.interfaces.ServicesSession) */
@Override
public void reportToAdmin(RatingAndReview ratingAndReview,
ServicesSession servicesSession) throws NMCServicesException,
TransportException {
LOG.debug("method: reportToAdmin");
// TODO Waiting for server
try {
OperationData reportOperation =
OperationDataCreator.createOperationData(ratingAndReview,
null,
servicesSession, OperationEnum.REPORT_TO_ADMIN,
ImporterType.BY_ID);

transportServices.sendOperation(reportOperation, true);
ObjectCreator.createBean(ratingAndReview, reportOperation);
LOG.debug("success: reportToAdmin");
} catch (ExporterException | ImporterException e) {
throw new RuntimeException(" Runtime exception ", e);
}
}

/* (non-Javadoc)
*
* @see
*
com.nomorecards.services.interfaces.RatingAndReviewServicesInterface#saveNewUserRat
ingAndReview(com.nomorecards
* .model.beans.RatingAndReview,
com.nomorecards.services.interfaces.ServicesSession) */
@Override
public void saveNewUserRatingAndReview(RatingAndReview ratingAndReview,
ServicesSession servicesSession) throws NMCServicesException,
TransportException {
LOG.debug("method: saveNewUserRatingAndReview");
try {
OperationData saveOperation =
OperationDataCreator.createOperationData(ratingAndReview,
null,
servicesSession, OperationEnum.ADD_EDIT_RATING_USER,
ImporterType.BY_GROUP);

transportServices.sendOperation(saveOperation, true);
ObjectCreator.createBean(ratingAndReview, saveOperation);
LOG.debug("success: saveNewUserRatingAndReview");
} catch (ExporterException | ImporterException e) {
throw new RuntimeException(" Runtime exception ", e);
}
}

/* (non-Javadoc)
*
* @see
*
com.nomorecards.services.interfaces.RatingAndReviewServicesInterface#saveNewItemRat
ingAndReview(com.nomorecards
* .model.beans.RatingAndReview,
com.nomorecards.services.interfaces.ServicesSession) */
@Override
public void saveNewItemRatingAndReview(RatingAndReview ratingAndReview,
ServicesSession servicesSession) throws NMCServicesException,
TransportException {
LOG.debug("method: saveNewItemRatingAndReview");
try {
OperationData saveOperation =
OperationDataCreator.createOperationData(ratingAndReview,
null,
servicesSession,
OperationEnum.ADD_EDIT_RATING_PRODUCT,
ImporterType.BY_GROUP);

transportServices.sendOperation(saveOperation, true);
ObjectCreator.createBean(ratingAndReview, saveOperation);
LOG.debug("success: saveNewItemRatingAndReview");

} catch (ExporterException | ImporterException e) {


throw new RuntimeException(" Runtime exception ", e);
}
}

/* (non-Javadoc)
*
* @see
*
com.nomorecards.services.interfaces.RatingAndReviewServicesInterface#saveRatingRepl
y(com.nomorecards.model.beans
* .RatingAndReview, com.nomorecards.services.interfaces.ServicesSession) */
@Override
public void saveRatingReply(RatingAndReview ratingAndReview,
ServicesSession servicesSession) throws NMCServicesException,
TransportException {
LOG.debug("method: saveRatingReply");

try {
OperationData addHelpTicket =
OperationDataCreator.createOperationData(ratingAndReview,
null,
servicesSession, OperationEnum.SAVE_RATING_REPLY,
ImporterType.BY_GROUP);

transportServices.sendOperation(addHelpTicket, true);
ObjectCreator.createBean(ratingAndReview, addHelpTicket);
LOG.debug("success: saveRatingReply");

} catch (ExporterException | ImporterException e) {


throw new RuntimeException(" Runtime exception ", e);
}

/* (non-Javadoc)
*
* @see
*
com.nomorecards.services.interfaces.RatingAndReviewServicesInterface#getItemRatingA
ndReviews(com.nomorecards
* .model.beans.User,
com.nomorecards.services.interfaces.RatingAndReviewServicesInterface.GetRatingAndRe
viewMode,
* com.nomorecards.model.filters.RatingAndReviewListFilter,
com.nomorecards.services.vo.PageIndexInfo,
* com.nomorecards.services.interfaces.ServicesSession) */
@Override
public PageList<RatingAndReview> getItemRatingAndReviews(User user,
GetRatingAndReviewMode mode, RatingAndReviewListFilter filter,
PageIndexInfo pageIndexInfo, ServicesSession servicesSession)
throws NMCServicesException, TransportException {
LOG.debug("method: getItemRatingAndReviews");
try {
OperationData getOperation =
OperationDataCreator.createOperationData(user, null,
servicesSession,
OperationEnum.GET_ITEM_RATINGS_AND_REVIEW,
ImporterType.BY_GROUP);
getOperation.addFilter(TycoonEnum.USER_PARENT_NMC_ID, user
.getNmcIdStr());

if (pageIndexInfo != null) {

getOperation.setStartLimit(pageIndexInfo.getStarItemIndex());
getOperation.setEndLimit(pageIndexInfo.getEndItemIndex());
} else {
getOperation.setStartLimit(0);
getOperation.setEndLimit(Long.MAX_VALUE);
}

transportServices.sendOperation(getOperation, true);
LOG.debug("success: getItemRatingAndReviews");

return (PageList<RatingAndReview>) new PageList<RatingAndReview>(


(List<RatingAndReview>) ObjectCreator.createBeanList(
RatingAndReview.class, getOperation), getOperation);
} catch (ImporterException | ExporterException e) {
throw new RuntimeException(" Runtime exception ", e);
}
}

/* ------------------------------------------------------------
* Object Methods - Metodos de Objeto
* ------------------------------------------------------------ */
}

You might also like