/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.discount.card;
import framework.beans.EntityDetails;
import framework.beans.FacadeBean;
import framework.beans.security.BeanRights;
import beans.discount.entity.DiscountCard;
import beans.discount.entity.DiscountCardDetails;
import beans.directory.simple.entities.DiscountCardType;
import beans.service.ServiceRender;
import beans.user.client.entity.Client;
import beans.user.collaborator.entities.Collaborator;
import framework.generic.ClipsServerException;
import framework.generic.EMoveToTrash;
import java.util.Date;
import javax.ejb.EJBException;
import javax.ejb.Stateful;
import beans.UserRightsSet;
import framework.audit.AuditDoc;
import java.util.List;
/**
* @security - Ok.
* @author axe
*/
@Stateful(mappedName="clips-beans/DiscountCardBean")
public class DiscountCardBean extends FacadeBean<DiscountCard>
implements DiscountCardBeanRemote {
//public static final UserRight RIGHT_CREATION = UserRightsSet.WRITE_CLIENT_PUBLIC_INFO;
public static int COMMAND_BLOCK = 4;
public DiscountCardBean() {
super(DiscountCard.class);
}
@Override
protected void initBeanRights() {
int[] r = new int[5];
r[COMMAND_READ] = RightPresence(UserRightsSet.READ_CLIENT_PUBLIC_INFO.id);
int cmr = RightPresence(UserRightsSet.WRITE_CLIENT_PUBLIC_INFO.id);
r[COMMAND_CREATE] = cmr;
r[COMMAND_MODIFY] = cmr;
r[COMMAND_REMOVE] = cmr;
r[COMMAND_BLOCK] = RightPresence(UserRightsSet.BLOCK_DISCOUNT_CARD.id);
rights = new BeanRights(r);
}
/**
* Обновляет данные сущности.
* @param details новые детали сущности
* @throws EJBException в случае если обновление отвергнуто системой
* безопастности либо произошла ошибка
* @return идентификатор сущности
*/
@Override
protected void onUpdate(DiscountCard entity, EntityDetails details,
AuditDoc auditDoc, List<AuditDoc> auditDocList) throws ClipsServerException{
DiscountCardDetails d = (DiscountCardDetails) details;
boolean onlyBlock = false;
if (entity.getId() != 0) {
//проверка на только блокирование
DiscountCardDetails dold = entity.getDetails(this);
DiscountCardDetails dnew = d.clone();
if (dold.collaboratorBlockerID == 0 && dnew.collaboratorBlockerID != 0) {
dnew.collaboratorBlockerID = 0;
if (dold.equals(dnew)) {
onlyBlock = true;
}
}
if (onlyBlock) {
//только блокировка
if (!isSuperUser()) checkCommandAccessibility(COMMAND_BLOCK);
} else {
//изменение данных
throwNeedAdminSecurityException("Обновлять дисконтную карту нельзя");
}
}
entity.setBlocker(d.collaboratorBlockerID == 0 ? null : findEntity(Collaborator.class, d.collaboratorBlockerID));
entity.setMoney(d.money);
entity.setLastused(d.lastUsed);
entity.setCollaborator(findEntity(Collaborator.class, getCollaboratorId()));
entity.setCreated(new Date());
entity.setClient(d.clientID == 0 ? null : findEntity(Client.class, d.clientID));
entity.setType(findEntity(DiscountCardType.class, d.typeID));
entity.setCode(d.code);
entity.setExpired(d.expired);
}
@Override
protected void onRemove(DiscountCard entity, List<AuditDoc> audit) throws ClipsServerException {
Field f[] = { new Field("discountCard", entity) };
if(getEntityCount(ServiceRender.class, f) > 0) {
throw new EMoveToTrash("По данной дисконтной карте есть оказанные услуги, удаление невозможно");
}
}
}