Package open.dolphin.master

Source Code of open.dolphin.master.InitDatabase

package open.dolphin.master;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.StringTokenizer;
import javax.naming.NamingException;
import open.dolphin.ejb.RemoteSystemService;
import open.dolphin.impl.login.LoginHelper;
import open.dolphin.infomodel.DepartmentModel;
import open.dolphin.infomodel.FacilityModel;
import open.dolphin.infomodel.IInfoModel;
import open.dolphin.infomodel.LicenseModel;
import open.dolphin.infomodel.RadiologyMethodValue;
import open.dolphin.infomodel.UserModel;
import open.dolphin.util.HashUtil;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

/**
* Dolphin用のマスタを登録する。
*
* @author Minagawa,Kazushi
*/
public class InitDatabase {
   
    private static final String MEMBER_TYPE = "FACILITY_USER";
    private static final String DEFAULT_FACILITY_OID = IInfoModel.DEFAULT_FACILITY_OID;
    private static final String PROFIEL_RESOURCE = "/open/dolphin/master/profiel.txt";
   
    // マスタデータファイル
    private static final String ADMIN_RESOURCE     = "/open/dolphin/master/admin-data-sjis.txt";
    private static final String ADMIN_COMMENT_RESOURCE   = "/open/dolphin/master/admin-coment-data-sjis.txt";
    private static final String RAD_METHOD_RESOURCE   = "/open/dolphin/master/radiology-method-data-sjis.txt";
   
    // マスタデータファイルの仕様
    private final int ARRAY_CAPACITY    = 20;
    private final int TT_VALUE          = 0;
    private final int TT_DELIM          = 1;
    private String delimitater     = "\t";
    private static final String ENCODING = "UTF-8";
   
    // SystemService to use
    private RemoteSystemService service;
   
    private Logger logger;
   
    /**
     * DolphinMasterMakerObject を生成する。
     */
    public InitDatabase(String userId, String password, String hostAddress) {
       
        logger = Logger.getLogger(this.getClass());
        BasicConfigurator.configure();
       
        addDatabaseAdmin(userId, password, hostAddress);
           
        addDolphinMaster();
       
        logger.info("データベースを初期化しました。");
    }
   
    public void addDatabaseAdmin(String userId, String password, String hostAddress) {
       
        HashMap<String, String> prop = new HashMap<String, String>();
       
        try {
            InputStream in = this.getClass().getResourceAsStream(PROFIEL_RESOURCE);
            InputStreamReader ir = new InputStreamReader(in, ENCODING);
            BufferedReader reader = new BufferedReader(ir);
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("#") || line.startsWith("!")) {
                    continue;
                }
                int index = line.indexOf("=");
                if (index < 0) {
                    continue;
                }
                String key = line.substring(0, index);
                String value = line.substring(index+1);
                System.out.println("key=" + key + "  value=" + value);
                if (value != null) {
                    prop.put(key, value);
                }
            }
           
            logger.info("管理者情報ファイルを読み込みました。");
            // RMI Connection
            String host = (hostAddress==null)? prop.get("host.address"): hostAddress;
            if (host == null || host.equals("")) {
                host = prop.get("host.address");
            }
           
            LoginHelper.setEJBClientContext(host, "4777", userId, password);
            this.service = (RemoteSystemService) LoginHelper.lookup("RemoteSystemService");
           
            logger.info("Host Service を取得しました。");
           
            // Admin Model
            FacilityModel facility = new FacilityModel();
            UserModel admin = new UserModel();
            admin.setFacilityModel(facility);

            // 施設OID
            facility.setFacilityId(DEFAULT_FACILITY_OID);

            facility.setFacilityName(prop.get("facility.name"));
            facility.setZipCode(prop.get("facility.zipcode"));
            facility.setAddress(prop.get("facility.address"));
            facility.setTelephone(prop.get("facility.telephone"));
            facility.setUrl(prop.get("facility.url"));
            Date date = new Date();
            facility.setRegisteredDate(date);
            facility.setMemberType(MEMBER_TYPE);

            //admin.setUserId(prop.get("admin.id"));
            //admin.setPassword(prop.get("admin.password"));
            if (userId == null || userId.equals("")) {
                admin.setUserId(prop.get("admin.login.id"));
            } else {
                admin.setUserId(userId);
            }
            if (password == null || password.equals("")) {
                admin.setPassword(prop.get("admin.login.password"));
            } else {
                admin.setPassword(password);
            }
            //String Algorithm = "MD5";
            //String encoding = "hex";
            //String charset = null;
            //String hashPass = CryptoUtil.createPasswordHash(Algorithm, encoding, charset, admin.getUserId(), admin.getPassword());
            String hashPass = HashUtil.MD5(admin.getPassword());
            admin.setPassword(hashPass);
            admin.setSirName(prop.get("admin.sir.name"));
            admin.setGivenName(prop.get("admin.given.name"));
            admin.setCommonName(admin.getSirName() + " " + admin.getGivenName());

            // 医療資格
            LicenseModel license = new LicenseModel();
            license.setLicense("doctor");
            license.setLicenseDesc("医師");
            license.setLicenseCodeSys("MML0026");
            admin.setLicenseModel(license);

            // 診療科
            DepartmentModel dept = new DepartmentModel();
            dept.setDepartment("01");
            dept.setDepartmentDesc("内科");
            dept.setDepartmentCodeSys("MML0028");
            admin.setDepartmentModel(dept);

            // Email
            String email = prop.get("admin.email");
            if (email == null || email.equals("")) {
                admin.setEmail(prop.get("someone@some-clinic.jp"));
            } else {
                admin.setEmail(email);
            }

            // MemberTpe
            admin.setMemberType(MEMBER_TYPE);

            // 登録日
            admin.setRegisteredDate(date);

            // 登録
            service.addFacilityAdmin(admin);

            logger.info("管理者を登録しました。");
           
           
        } catch (IOException e) {
            logger.fatal("管理者情報ファイルを読み込めません。");
            e.printStackTrace();
            System.exit(1);
        } catch (NamingException ne) {
            logger.fatal("ホストに接続できません。");
            ne.printStackTrace();
            System.exit(1);
        } catch (Exception ee) {
            logger.fatal("管理者情報の登録に失敗しました。");
            ee.printStackTrace();
            System.exit(1);
        }
           
    }
   
   
   
    /**
     * マスタを登録する。
     */
    public void addDolphinMaster() {
        try {
            addRdMethod(RAD_METHOD_RESOURCE);
        } catch (IOException e) {
            logger.fatal("マスターファイルを読み込めません。");
            e.printStackTrace();
            System.exit(1);
           
        } catch (Exception ee) {
            logger.fatal("マスターファイルの登録に失敗しました。");
            ee.printStackTrace();
            System.exit(1);
        }
       
    }
   
   
    /**
     * 放射線メソッドマスタを登録する。
     * @param name 放射線メソッドマスタリソース名
     */
    private void addRdMethod(String name) throws IOException {
       
        //try {
            InputStream in = this.getClass().getResourceAsStream(name);
            InputStreamReader ir = new InputStreamReader(in, ENCODING);
            BufferedReader reader = new BufferedReader(ir);
           
            String line = null;
            ArrayList<RadiologyMethodValue> list = null;
            int cnt = 0;
           
            while ( (line = reader.readLine()) != null ) {
               
                String[] data = getStringArray(line);
               
                if (data != null) {
                   
                    RadiologyMethodValue av = new RadiologyMethodValue();
                    av.setHierarchyCode1(format(data[0]));
                    av.setHierarchyCode2(format(data[1]));
                    av.setMethodName(format(data[2]));
                   
                    if (list == null) {
                        list = new ArrayList<RadiologyMethodValue>();
                    }
                    list.add(av);
                   
                    cnt++;
                }
            }
           
            if (list == null) {
                return;
            }
           
            service.putRadMethodMaster(list);
            logger.info("放射線メソッドマスタを登録しました。");
           
        //} catch (Exception e) {
           // e.printStackTrace();
        //}
    }
   
    /**
     * 文字を整形する。
     */
    private String format(String d) {
        if (d == null) {
            return null;
        } else if (d.equals("\\N")) {
            return null;
        } else {
            return d;
        }
    }
   
    /**
     * リソースファイルから読み込んだタブ区切りの1行をパースし、 String 配列のデータにして返す。
     * @param line パースするライン
     * @return データ配列
     */
    private String[] getStringArray(String line) {
       
        if (line == null) {
            return null;
        }
       
        String[] ret = new String[ARRAY_CAPACITY];
        int count = 0;
       
        StringTokenizer st = new StringTokenizer(line, delimitater, true);
        int state = TT_VALUE;
       
        while (st.hasMoreTokens()) {
           
            if ( (count % ARRAY_CAPACITY) == 0 ) {
                String[] dest = new String[count + ARRAY_CAPACITY];
                System.arraycopy(ret, 0, dest, 0, count);
                ret = dest;
            }
           
            String token = st.nextToken();
           
            switch (state) {
               
                case TT_VALUE:
                    if (token.equals(delimitater)) {
                        token = null;
                       
                    } else {
                        state = TT_DELIM;
                    }
                    ret[count] = token;
                    count++;
                    break;
                   
                case TT_DELIM:
                    state = TT_VALUE;
                    break;
            }
        }
       
        String[] ret2 = new String[count];
        System.arraycopy(ret, 0, ret2, 0, count);
       
        return ret2;
    }
   
    public static void main(String[] args) {
        String usage = "Usage: java -cp OpenDolphin-1.3.0.X.jar open.dolphin.master.InitDatabase id password hostAddress";
        if (args.length == 3) {
            new InitDatabase(args[0], args[1], args[2]);
           
        } if (args.length == 0) {
            // load data from profiel.txt
            new InitDatabase(null, null, null);
           
        } else {
            System.out.println(usage);
        }
        System.exit(0);
    }
}
TOP

Related Classes of open.dolphin.master.InitDatabase

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.