/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.discount;
import framework.beans.SecuredBean;
import framework.beans.security.BeanRights;
import framework.generic.ClipsServerException;
import beans.directory.discount.entity.DiscountLevel;
import beans.directory.simple.entities.DiscountCardType;
import beans.directory.simple.entities.ServiceClass;
import beans.discount.entity.Discount;
import beans.discount.entity.DiscountDetails;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateful;
import beans.UserRightsSet; import framework.security.RightChecker;
/**
* @security ok
* @author axe
*/
@Stateful(mappedName="clips-beans/DiscountBean")
public class DiscountBean extends SecuredBean
implements DiscountBeanRemote {
public static int COMMAND_READ = 0;
public static int COMMAND_WRITE = 1;
@Override
protected void initBeanRights() {
int [] r = new int[2];
r[COMMAND_READ] = RightPresence(UserRightsSet.READ_DISCOUNT.id);
r[COMMAND_WRITE] = RightPresence(UserRightsSet.WRITE_DISCOUNT.id);
rights = new BeanRights(r);
}
@Override
public List<DiscountDetails> getDetails(int discountCardTypeID) throws ClipsServerException {
checkCommandAccessibility(COMMAND_READ);
DiscountCardType type = findEntity(DiscountCardType.class, discountCardTypeID);
Collection<Discount> discounts = findEntityList(Discount.class, "cardType", type);
List<DiscountDetails> list = new ArrayList<DiscountDetails>();
Iterator<Discount> i = discounts.iterator();
while(i.hasNext()) {
Discount j = (Discount) i.next();
list.add(j.getDetails((RightChecker) this));
}
return list;
}
@Override
public void update(int discountCardTypeID, List<DiscountDetails> list) throws ClipsServerException {
checkCommandAccessibility(COMMAND_WRITE);
DiscountCardType type = findEntity(DiscountCardType.class, discountCardTypeID);
for(int i=0; i<list.size(); i++) {
DiscountDetails d = list.get(i);
DiscountLevel level = findEntity(DiscountLevel.class, d.level);
ServiceClass cls = findEntity(ServiceClass.class, d.cls);
Field field[] = {
new Field("cardType", type),
new Field("serviceClass", cls),
new Field("level", level)};
@SuppressWarnings("unchecked")
Iterator<Discount> discounts = findEntityList(Discount.class, field).iterator();
if(discounts.hasNext()) {
Discount discount = discounts.next();
discount.setValue(d.value);
manager.merge(discount);
// remove extra lines
while(discounts.hasNext()) {
discount = discounts.next();
manager.remove(discount);
}
} else {
Discount discount = new Discount();
discount.setDiscountCardType(type);
discount.setServiceClass(cls);
discount.setLevel(level);
discount.setValue(d.value);
manager.persist(discount);
}
}
}
}