Package framework.beans.address

Source Code of framework.beans.address.DirectoryKladrTypeBean

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package framework.beans.address;

import framework.beans.directory.DirectoryBean;
import framework.beans.address.entities.AddressObjectType;
import framework.beans.address.entities.AddressObjectTypeDetails;
import framework.generic.ClipsServerException;
import framework.generic.EDataIntegrity;
import framework.security.UserRight;
import framework.security.UserRightsSetAbstract;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.Stateful;

/**
* @security Ok.
* @author axe
* Типы справок
*/
@Stateful(mappedName="clips-beans/DirectoryKladrTypeBean")
public class DirectoryKladrTypeBean extends DirectoryBean<AddressObjectType, AddressObjectTypeDetails>
        implements DirectoryKladrTypeBeanRemote {
   
    public DirectoryKladrTypeBean() {
        super(AddressObjectType.class, "типы адресуемых объектов");
    }

    @Override
    protected UserRight getRightForCreateDirectoryItem() {
        return UserRightsSetAbstract.WRITE_REGION_ADMIN_DIRECTORY;
    }

    @Override
    protected UserRight getRightForWriteToDirectory() {
        return UserRightsSetAbstract.WRITE_REGION_ADMIN_DIRECTORY;
    }

    @Override
    protected void set(AddressObjectType entity, AddressObjectTypeDetails details) {
        AddressObjectType aot = entity;
        AddressObjectTypeDetails d = details;
        aot.setTitle(details.title);
        aot.setAbbrivation(d.abbrivation);
        aot.setIsVillage(d.isVillage);
        aot.setLevel(d.level);
        aot.setCode(d.code);
        aot.setExtKey(d.external_key);
    }

    /**
     *
     * @param entity
     * @throws generic.ClipsServerException
     */
    @Override
    protected void onRemove(AddressObjectType entity) throws ClipsServerException {
        throw new EDataIntegrity("Запрещено");
    }
   
    /**
     *
     * @param details
     */
    @Override
    protected void onAppend(AddressObjectTypeDetails details) throws ClipsServerException {
        throw new EDataIntegrity("Запрещено");
    }


    /**
     * Внимание! ничего не удаляет (и даже не пытается)
     * @param types
     * @throws ClipsServerException
     */
    @Override
    public void syncAddressObjectTypes(List<AddressObjectTypeDetails> types) throws ClipsServerException {
        Set<Integer> codes2update = new HashSet<Integer>();
        //map<код, сущность> сущности которые будут обновлены
        Map<Integer, AddressObjectType> entitys2Update = new HashMap<Integer, AddressObjectType>();

        for (AddressObjectTypeDetails d : types) {
            codes2update.add(d.code);
        }
        Field[] f = new Field[] {new Field("code", codes2update, Field.OPERATOR_IN)};
        //выберем уже лежащие в базе сущности
        Iterator it = findEntityList(AddressObjectType.class, f).iterator();
        //и загоним их в мап
        while (it.hasNext()) {
            AddressObjectType type = (AddressObjectType)it.next();
            entitys2Update.put(type.getCode(), type);
        }

        //теперь добавляем сущности в базу (существующие обновим взяв из мапа)
        for (AddressObjectTypeDetails d : types) {
            AddressObjectType type = entitys2Update.get(d.code);
            if (type == null){
                type = new AddressObjectType();
                type.setAbbrivation(d.abbrivation);
                type.setCode(d.code);
                type.setLevel(d.level);
                type.setTitle(d.title);
                manager.persist(type);
            }else{
                type.setAbbrivation(d.abbrivation);
                type.setCode(d.code);
                type.setLevel(d.level);
                type.setTitle(d.title);
                manager.merge(type);
            }
        }
        manager.flush();
    }

}
TOP

Related Classes of framework.beans.address.DirectoryKladrTypeBean

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.