/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.directory.vidal.atc;
import beans.UserRightsSet;
import beans.directory.vidal.entities.atc.ClassificationAtcDetails;
import framework.beans.directory.DirectoryBean;
import beans.directory.vidal.entities.atc.ClassificationAtc;
import beans.directory.vidal.entities.atc.VidalAtc;
import beans.directory.vidal.entities.atc.VidalAtcPK;
import framework.generic.ClipsServerException;
import framework.generic.EDataIntegrity;
import framework.security.UserRight;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import javax.ejb.Stateful;
/**
*
* @author petr
*/
@Stateful(mappedName="clips-beans/DirectoryVidalAtcBean")
public class DirectoryVidalAtcBean extends DirectoryBean<ClassificationAtc, ClassificationAtcDetails> implements DirectoryVidalAtcBeanRemote {
public DirectoryVidalAtcBean() {
super(ClassificationAtc.class, "Видаль, классификация АТХ");
}
@Override
protected UserRight getRightForCreateDirectoryItem() {
return UserRightsSet.WRITE_REGION_ADMIN_DIRECTORY;
}
@Override
protected UserRight getRightForWriteToDirectory() {
return UserRightsSet.WRITE_REGION_ADMIN_DIRECTORY;
}
@Override
protected void set(ClassificationAtc atc, ClassificationAtcDetails item) throws ClipsServerException {
ClassificationAtc atcParent = item.parentItem == 0? null: findEntity(ClassificationAtc.class, item.parentItem);
atc.setTitle(item.title);
//atc.setDirty(details.dirty);
atc.setGroupcode(item.groupCode);
if (atcParent != null){
atc.setParentAtc(atcParent);
}
}
@Override
protected void onRemove(ClassificationAtc entity) throws ClipsServerException {
Field f[] = { new Field("atc", entity) };
if(getEntityCount(VidalAtc.class, f, "") > 0) {
throw new EDataIntegrity("Данному классификатору соотвествует "
+ "несколько элементов классификатора VIDAL, удаление невозможно");
}
}
@Override
public Set<Integer> getVidals(int atc) throws ClipsServerException {
if(atc == 0) {
throw new EDataIntegrity("");
}
Iterator<VidalAtc> list = findEntityList(VidalAtc.class, "key.atc", atc).iterator();
Set<Integer> res = new HashSet<Integer>();
while(list.hasNext()) {
VidalAtc vidal = list.next();
res.add(vidal.getKey().getVidal());
}
return res;
}
@Override
public void setVidals(int atc, Set<Integer> vidals) throws ClipsServerException {
checkCommandAccessibility(COMMAND_WRITE);
Set<Integer> backup = new HashSet<Integer>(vidals);
Set<Integer> oldVidals = getVidals(atc);
vidals.removeAll(oldVidals);
oldVidals.removeAll(backup);
if(oldVidals.size() > 0) {
@SuppressWarnings("unchecked")
Field f[] = {
new Field("key.atc", atc),
new Field("key.vidal", oldVidals, Field.OPERATOR_IN)
};
deleteEntityList(VidalAtc.class, f);
}
Iterator<Integer> addNew = vidals.iterator();
while(addNew.hasNext()) {
VidalAtc r = new VidalAtc();
r.setKey(new VidalAtcPK(atc, addNew.next()));
System.out.println("Added new:" + r);
manager.persist(r);
}
}
}