Package beans.directory.vidal

Source Code of beans.directory.vidal.ClassificatorsBean

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

package beans.directory.vidal;

import framework.beans.SecuredBean;
import framework.beans.security.BeanRights;
import beans.directory.mkb10.entity.Mkb10;
import beans.directory.vidal.entities.Vidal;
import beans.directory.vidal.entities.atc.ClassificationAtc;
import beans.directory.vidal.entities.atc.VidalAtc;
import beans.directory.vidal.entities.atc.VidalAtcPK;
import beans.directory.vidal.entities.farm.ClassificationFarm;
import beans.directory.vidal.entities.farm.VidalFarm;
import beans.directory.vidal.entities.farm.VidalFarmPK;
import beans.directory.vidal.entities.matter.Matter;
import beans.directory.vidal.entities.matter.VidalMatter;
import beans.directory.vidal.entities.matter.VidalMatterPK;
import beans.directory.vidal.entities.mkb10.VidalMkb10;
import beans.directory.vidal.entities.mkb10.VidalMkb10PK;
import framework.generic.ClipsServerException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import javax.ejb.EJBException;
import javax.ejb.Stateful;
import beans.UserRightsSet;
import framework.utils.Pair;

/**
* @security ok.
* @author axe
*/
@Stateful(mappedName="clips-beans/ClassificatorsBean")
public class ClassificatorsBean extends SecuredBean
        implements ClassificatorsBeanRemote {

    public static final int COMMAND_WRITE_CLASSIFICATORS = 0;
    ClassificatorsChunk cc;

    /**              title   ID*/
    private HashMap<String, Integer> mapVidal;
    /**              title   ID*/
    private HashMap<String, Integer> mapMatter;
    /**Список пар ClassificationAtc, VidalID    */
    private ArrayList<Pair<ClassificationAtc, Integer>> atcVidalList;
    /**Список пар ClassificationFarm, VidalID    */
    private ArrayList<Pair<ClassificationFarm, Integer>> farmVidalList;
   
    @Override
    protected void initBeanRights() throws ClipsServerException {
        int r[] = new int[1];
        r[COMMAND_WRITE_CLASSIFICATORS] = RightPresence(UserRightsSet.WRITE_REGION_ADMIN_DIRECTORY.id);
        rights = new BeanRights(r);
    }
   
    /**
     * Очистка всех связей классификаторов и активных веществ с лекарствами
     * Очистка АТХ и Клин-Фарм классификатора
     * @throws generic.ClipsServerException
     */
    @Override
    public void clearAll() throws ClipsServerException {
        checkCommandAccessibility(COMMAND_WRITE_CLASSIFICATORS);
        clearAtcVidal();
        clearFarmVidal();
        clearMKB10Vidal();
        clearMatterVidal();
        clearFarm();
        clearAtc();
    }
   
    /**
     * Очищает таблицу связей МКБ10 и лекарств
     */
    private void clearMKB10Vidal() throws ClipsServerException {
        deleteEntityList(VidalMkb10.class, null);
    }
    /**
     * Очищает таблицу связей классификатора АТХ и лекарств
     * @throws generic.ClipsServerException
     */
    private void clearAtcVidal() throws ClipsServerException {
        deleteEntityList(VidalAtc.class, null);
    }
   
    /**
     * Очищает таблицу связей классификатора Фарм и лекарств
     * @throws generic.ClipsServerException
     */
    private void clearFarmVidal() throws ClipsServerException {
        deleteEntityList(VidalFarm.class, null);
    }

    /**
     * Очищает таблицу связей активных веществ и лекарств
     * @throws generic.ClipsServerException
     */
    private void clearMatterVidal() throws ClipsServerException {
        deleteEntityList(VidalMatter.class, null);
    }

    /**
     * Полностью очищает классификатор Фарм
     * @throws generic.ClipsServerException
     */
    private void clearFarm() throws ClipsServerException {
        deleteEntityList(ClassificationFarm.class, null);
    }

    /**
     * Полная очистка классификатора
     * @throws generic.ClipsServerException
     */
    private void clearAtc() throws ClipsServerException {
        deleteEntityList(ClassificationAtc.class, null);
    }
   
    @Override
    public void loadAll(ClassificatorsChunk cc) throws ClipsServerException {
        this.cc = cc;
        loadMatter();
        manager.flush();
        loadVidal();
        manager.flush();
        loadVidalMatter();
        manager.flush();
        loadATC();
        manager.flush();
        loadKF();
        manager.flush();
        loadMKB();
    }
   
    private void loadMatter() {
        //загрузка в мап уже находящихся в базе объектов
        mapMatter = new HashMap<String, Integer>();
        List matterEntityList = findEntityList(Matter.class);
        for (int i = 0; i < matterEntityList.size(); i++) {
            Matter matter = (Matter) matterEntityList.get(i);
            mapMatter.put(matter.getTitle().toUpperCase().trim(), matter.getId());
        }
        //сохранение в базу новых объектов
        Collection<String> newMatterTitles = cc.matterMap.values();
        for (Iterator<String> it = newMatterTitles.iterator(); it.hasNext();) {
            String matterTitle = it.next().toUpperCase().trim();
            //if (mapMatter.get(matterTitle) == null) {
            if (!mapMatter.containsKey(matterTitle)) {
                //Сохранение в базу и запоминание в мап
                Matter matter = new Matter();
                matter.setTitle(matterTitle);
                manager.persist(matter);
                manager.flush();
                manager.refresh(matter);
                mapMatter.put(matterTitle, matter.getId());
                //System.out.println(matter.getId() + " - " + matterTitle);
            }
        }
       
    }

    private void loadVidal() {
        mapVidal = new HashMap<String, Integer>();
        List vidalEntityList = findEntityList(Vidal.class);
        for (int i = 0; i < vidalEntityList.size(); i++) {
            Vidal vidal = (Vidal) vidalEntityList.get(i);
            mapVidal.put(vidal.getTitle().toUpperCase().trim(), vidal.getId());
        }
        //сохранение в базу новых объектов
        Collection<String> newVidalTitles = cc.vidalMap.values();
        for (Iterator<String> it = newVidalTitles.iterator(); it.hasNext();) {
            String vidalTitle = it.next().toUpperCase().trim();
            if (!mapVidal.containsKey(vidalTitle)) {
                //Сохранение в базу и запоминание в мап
                Vidal vidal = new Vidal();
                vidal.setTitle(vidalTitle);
                manager.persist(vidal);
                manager.flush();
                manager.refresh(vidal);
                mapVidal.put(vidalTitle, vidal.getId());
                //System.out.println(vidal.getId() + " - " + vidalTitle);
            }
        }
    }
   
    private void loadVidalMatter() {
        cc.vidalMatter.iterator();
        for (Iterator<Pair<String,String>> it = cc.vidalMatter.iterator(); it.hasNext();) {
            Pair<String, String> pair = it.next();
            String vidal = pair.first.toUpperCase().trim();
            String matter = pair.second.toUpperCase().trim();
            Integer vidalID = mapVidal.get(vidal);
            Integer matterID = mapMatter.get(matter);
            if (vidalID == null){
                throw new EJBException("Неизвестное лекарство : " + vidal);
            } else if (matterID == null) {
                throw new EJBException("Неизвестное активное вещество : " + matter);
            } else {
                VidalMatter r = new VidalMatter();
                r.setKey(new VidalMatterPK(vidalID, matterID));
                //System.out.println("Added new:" + r);
                manager.persist(r);
                //manager.flush();
            }
        }
    }
   
    private void loadATC() throws ClipsServerException {
        atcVidalList = new ArrayList<Pair<ClassificationAtc, Integer>>();
        TreeNode root = cc.atcMap.values().iterator().next();
        while (root.getParent() != null) {
            root = root.getParent();
        }
        //System.out.println("Root : " + root);
        for (int i = 0; i < root.getChidren().size(); i++) {
            TreeNode treeNode = root.getChidren().get(i);
            //System.out.println("    " + treeNode);
            saveNodeAtc(treeNode, null);
        }
        manager.flush();
        manager.flush();
        manager.flush();
        manager.flush(); //Лёхины прыжки с бубеном вправо
        for (int i = 0; i < atcVidalList.size(); i++) {
            Pair<ClassificationAtc, Integer> pair = atcVidalList.get(i);
            //System.err.println("Added : " + pair);
            manager.refresh(pair.first);
            VidalAtcPK pk = new VidalAtcPK(pair.first.getId(), pair.second);
            VidalAtc r = new VidalAtc();
            r.setKey(pk);
            manager.persist(r);
        }
        manager.flush();
    }
   
    private void saveNodeAtc(TreeNode node, ClassificationAtc parentAtc) throws ClipsServerException {
        ClassificationAtc atc = new ClassificationAtc();
        atc.setGroupcode(node.getCode());
        atc.setTitle(node.getTitle());
        atc.setParentAtc(parentAtc);
        manager.persist(atc);
        ArrayList<TreeNode> chidren = node.getChidren();
        for (int i = 0; i < chidren.size(); i++) {
            TreeNode treeNode = chidren.get(i);
            saveNodeAtc(treeNode, atc);
        }
        //Связи с лекарствами
        Set<String> vidalTitles = cc.atcVidals.get(node);
        if (vidalTitles != null) {
            for (Iterator<String> it = vidalTitles.iterator(); it.hasNext();) {
                String title = it.next().toUpperCase().trim();
                Integer vidalID = mapVidal.get(title);
                Pair<ClassificationAtc, Integer> pair = new Pair<ClassificationAtc, Integer>(atc, vidalID);
                atcVidalList.add(pair);
            }
        }
    }
   
    private void loadKF() throws ClipsServerException {
        farmVidalList = new ArrayList<Pair<ClassificationFarm, Integer>>();
        TreeNode root = cc.kfMap.values().iterator().next();
        while (root.getParent() != null) {
            root = root.getParent();
        }
        //System.out.println("Root : " + root);
        for (int i = 0; i < root.getChidren().size(); i++) {
            TreeNode treeNode = root.getChidren().get(i);
            //System.out.println("    " + treeNode);
            saveNodeKF(treeNode, null);
        }
        manager.flush();manager.flush();manager.flush();manager.flush(); //Лёхины прыжки с бубеном вправо
        for (int i = 0; i < farmVidalList.size(); i++) {
            Pair<ClassificationFarm, Integer> pair = farmVidalList.get(i);
            //System.err.println("Added : " + pair);
            manager.refresh(pair.first);
            VidalFarmPK pk = new VidalFarmPK(pair.first.getId(), pair.second);
            VidalFarm r = new VidalFarm();
            r.setKey(pk);
            manager.persist(r);
        }
        manager.flush();

    }
   
    private void saveNodeKF(TreeNode node, ClassificationFarm parentFarm) throws ClipsServerException {
        ClassificationFarm farm = new ClassificationFarm();
        farm.setTitle(node.getTitle());
        farm.setParentFarm(parentFarm);
        manager.persist(farm);
        ArrayList<TreeNode> chidren = node.getChidren();
        for (int i = 0; i < chidren.size(); i++) {
            TreeNode treeNode = chidren.get(i);
            saveNodeKF(treeNode, farm);
        }
        //Связи с лекарствами
        Set<String> vidalTitles = cc.farmVidals.get(node);
        if (vidalTitles != null) {
            for (Iterator<String> it = vidalTitles.iterator(); it.hasNext();) {
                String title = it.next().toUpperCase().trim();
                Integer vidalID = mapVidal.get(title);
                Pair<ClassificationFarm, Integer> pair = new Pair<ClassificationFarm, Integer>(farm, vidalID);
                farmVidalList.add(pair);
            }
        }
    }

    private void loadMKB() throws ClipsServerException {
        Set<Entry<String, Set<String>>> entrySet = cc.mkbVidals.entrySet();
        //хеш   код     mkbID
        HashMap<String, Integer> mapMKB = new HashMap<String, Integer>();
        List mkbList = findEntityList(Mkb10.class);
        for (int i = 0; i < mkbList.size(); i++) {
            Mkb10 mkb10 = (Mkb10) mkbList.get(i);
            //System.out.println(mkb10);
            //if (mapMKB.containsKey(mkb10.getCode())) {
                //throw new ClipsServerException ("Должен быть только 1 элемент МКБ10 с данным кодом " + mkb10.getCode());
            //}
            mapMKB.put(mkb10.getCode(), mkb10.getId());
        }
        //throw new ClipsServerException ("Должен быть только 1 элемент МКБ10 с данным кодом " + mkb10.getCode());
       
        for (Iterator<Entry<String, Set<String>>> it = entrySet.iterator(); it.hasNext();) {
            Entry<String, Set<String>> entry = it.next();
            String code = entry.getKey();
            Integer mkbID;
            mkbID= mapMKB.get(code);
            if (mkbID == null) {
                mkbID= mapMKB.get(code + "+");
            }
            if (mkbID == null) {
                mkbID= mapMKB.get(code + "*");
            }
            if (mkbID == null) {
                throw new ClipsServerException ("Нет элемента МКБ10 с данным кодом " + code);
            }
            Set<String> vidalTitles = entry.getValue();
            for (Iterator<String> it1 = vidalTitles.iterator(); it1.hasNext();) {
                String title = it1.next().toUpperCase().trim();
                Integer vidalID = mapVidal.get(title);
                VidalMkb10PK pk = new VidalMkb10PK(mkbID, vidalID);
                //System.out.println("Added " + pk);
                VidalMkb10 r = new VidalMkb10();
                r.setKey(pk);
                manager.persist(r);
            }
        }
    }
}
TOP

Related Classes of beans.directory.vidal.ClassificatorsBean

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.