All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.enterprise.util.PropertiesToADomainFormat Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
/*******************************************************************************
 * Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     s IT Solutions AT Spardat GmbH - initial API and implementation
 *******************************************************************************/

/*
 * @(#) $Id: PropertiesToADomainFormat.java 2093 2007-11-28 14:23:36Z s3460 $
 *
 * Copyright 2004/2005 by SPARDAT Sparkassen-Datendienst Ges.m.b.H.,
 * A-1110 Wien, Geiselbergstr.21-25.
 * All rights reserved.
 *
 */
package at.spardat.enterprise.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.StringTokenizer;

/**
 * This class (with a main method) transforms files in the old java .properties
 * format to the new ADomain format.
 * As input to the main method the directory with the .properties files has to be stated.
 * Under this directory a new directory 'newdomains' with the domains in the new format is created.  
 * Example old format (ress):
 *               # example of a ress-type .properties-file
                 fe=female
                 ma=male
                 un=unknown

 * Example new format (res/rsc):
 *               # example of a res-type .properties-file
                 COLS=COD_KEY,SHORT_VALUE,LONG_VALUE,VALID_FROM,VALID_TO,STATUS
                 10="fe","fe","weiblich" 
                 20="ma","ma","maennlich"
                 30="un","un","unbekannt"

For the filters as used in the EBV a special behauvior is implemented:
The EBV has domain entries like '_FILTER_VERFUEGER=0,2,1,S,3' specifying a filter.
For entries like these a new domain file in the new format with only the values from the filter
is generated. This file has a name like example 'originaldomain_verfueger.properties'. 
Right now this behauvior cannot be changed by configuration, this may be implemented in a later version.

 * @author s3460  
 * @since version_number
 */
public class PropertiesToADomainFormat {
    
    private static String HEADER = "COLS=COD_KEY,SHORT_VALUE,LONG_VALUE,VALID_FROM,VALID_TO,STATUS";
    
    private static String PROPERTIES =".properties";
    
    private static String EBV_FILTER ="_FILTER_";
    
    private static String EBV_BITLIST ="BITLIST"; 
    
    private File outputDir;
    
    public PropertiesToADomainFormat() {
        super();        
    }
    
    /**
     * Reads a directory with .properties files representing domains in the old .properties format.  
     * @param directory
     * @since version_number
     * @author s3460
     */
    public void read(String directory){
        PrintWriter out = null;
        String outFileName = null;               

        try {
            File dir = new File(directory);
            if (!dir.exists() || dir.isFile()) {
                log(directory
                        + " does not exist or is no directory ! - processing of this directory skipped.");
                return;
            }
            
            outputDir = new File(directory + File.separator + "newdomains");
            outputDir.mkdir();
            
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if(file.getName().endsWith(PROPERTIES)){
                    readFile(file);
                }
            }            

            log(directory + " finished.");            
        
        } catch (Exception e) {
            //log(e.getMessage());
            e.printStackTrace();

        } finally {
            
        }
    }
    
    /**
     * Reads a file in the old domain .properties format.
     * @param file
     * @throws Exception
     * @since version_number
     * @author s3460
     */
    private void readFile(File file) throws Exception {
        BufferedReader in = null;
        PrintWriter out = null;
        LinkedHashMap map = new LinkedHashMap();
        String line = null;
            
        try {
            in = new BufferedReader(new FileReader(file));
            String outFileName = outputDir.getAbsolutePath() + File.separator + file.getName();
            out = new PrintWriter(new BufferedWriter(new FileWriter(outFileName)));
            //log("Start reading directory file: " + filename);
            

            while ((line = in.readLine()) != null) {
                if(line.startsWith("#") || line.trim().length()==0){
                    continue;
                }
                if(line.startsWith(EBV_BITLIST)){
                    log(EBV_BITLIST + " entry at reading file ignored, file " + file.getName() + " line: " +line);
                    continue;
                }
                int index = line.indexOf('=');
                String key = line.substring(0,index).trim();
                String value = line.substring(index+1).trim();
                map.put(key, value);
            }
            
            writeFile(out,map,outFileName);
        
        } catch (Exception e) {
            log("Exception at reading file " + file.getName() + " line: " +line);
            e.printStackTrace();
        }finally{
            if(in != null)
                in.close();
            if(out != null)
                out.close();
        }
        
    }

    /**
     * Writes the file in the new ADomain format 
     * @param out
     * @param map
     * @param outFileName
     * @since version_number
     * @author s3460
     */
    private void writeFile(PrintWriter out, LinkedHashMap map, String outFileName) {
        try{
            out.println(HEADER);
            int col = 10;
            for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
                String key = (String) iter.next();
                String value = (String) map.get(key);
                if(key.startsWith(EBV_FILTER)){
                    if(outFileName != null)
                        writeFilterDomain(key,map, outFileName);
                }else{
                    //30="e","e","eingeschr?nkt"
                    out.println(col+"=\""+key+"\",\""+key+"\",\""+value+"\"");                
                    col += 10;
                }            
            }
        }catch(Exception ex){
            log("Exception at Writing File stream " + out.toString());
            ex.printStackTrace();
        }
        
    }

    /**
     * If an EBV filter entry like "_FILTER_INSTITUT=0,1,2" ocurred
     * then a new ADomain file in the new format is generated by using the writeFile() method.
     * @param key
     * @param map
     * @param outFileName
     * @since version_number
     * @author s3460
     */
    private void writeFilterDomain(String key, LinkedHashMap map, String outFileName) {
        String filterName = key.substring(EBV_FILTER.length());
        PrintWriter out = null;  
        LinkedHashMap filterMap = new LinkedHashMap();
            
        try {
            String fileName = 
                outFileName.substring(0, outFileName.length()-PROPERTIES.length())
                + "_" + filterName.toLowerCase() + PROPERTIES;
            out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
            
            String values = (String) map.get(key);
            StringTokenizer st = new StringTokenizer(values,",");
            while (st.hasMoreTokens()) {
                String filterKey = st.nextToken();
                filterMap.put(filterKey, map.get(filterKey));
            }
            
            writeFile(out,filterMap,null);
        
        } catch (Exception e) {
            log(e.getMessage());
            e.printStackTrace();
        }finally{            
            if(out != null)
                out.close();
        }
    }

    private void log(String mess){
        System.out.println(mess);
    }

    /**
     * @param args
     * @since version_number
     * @author s3460
     */
    public static void main(String[] args) {
        PropertiesToADomainFormat p = new PropertiesToADomainFormat();
        p.read(args[0]);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy