2016-07-29 15 views
0

i have attached screen shot of the resultant CSV fileIch versuche, das Ergebnis der dql-Abfragen in eine separate CSV-Datei mit DFC-Codierung zu schreiben.

Ich versuche, das Ergebnis der dql-Abfragen in eine separate CSV-Datei mithilfe von DFC-Codierung zu schreiben. aber das Ergebnis des ersten Datensatzes ist das doppelte Drucken im resultierenden Blatt.

public class ChkGroupExistence {

/** 
* @param args 
*/ 
private static Properties queryProp; 
//private static Properties configProp; 
IDfSession sess = null; 


public ChkGroupExistence() { 
    System.out.println("Loading Properties.."); 
    LoadProps loadProp = LoadProps.getInstance(); 
    queryProp = loadProp.getQueryProp(); 
    //configProp = loadProp.getConfigProp(); 
    List<String> proj_list = new ArrayList<String>(); 
    List<String> grp_list = new ArrayList<String>(); 
    List<String> acl_list = new ArrayList<String>(); 

    HashMap<String, String> projList_Map = new HashMap<String, String>(); 

    IDfCollection projId_coll = null; 
    IDfCollection grp_coll = null; 
    //IDfCollection chk_coll = null; 
    //IDfCollection acl_coll = null; 

    String grpqry = null; 

    String chkqry = null; 

    //String getACLQuery = null; 

    int j=0; 
    CreateSession ifcDocsDfSession = new CreateSession(); 
    try { 
     sess = ifcDocsDfSession.getSession(); 
     DfLogger.info(this, "Session Created ::" + sess.getSessionId(), 
       null, null); 
    } catch (DfException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    String qry = queryProp 
      .getProperty(IdocsConstants.PROJECT_ID); 
    try { 
     CSVWriter csvwriter=new CSVWriter(new FileWriter(new File("C:\\WBG\\IFCTools\\cs_log.csv"))); 

     projId_coll = Util.executeQuery(sess, qry, IDfQuery.READ_QUERY); 

     while (projId_coll.next()) { 
      proj_list.add(projId_coll.getString("project_id")); 
     } 

     System.out.println("List of Project ::"+proj_list.size()); 
     String tempQuery=queryProp.getProperty(IdocsConstants.P_GROUP_EXIST); 
     //String tempQuery1=queryProp.getProperty(IdocsConstants.P_GROUP_VERIFY); 
     //String tempQuery2 = queryProp.getProperty(IdocsConstants.P_GETACL); 


     List<String[]> csvList=new ArrayList<String[]>(); 
     List<String[]> titleList = new ArrayList<String[]>(); 

     String[] projList; 
     String[] titleString; 

     titleString = new String[3]; 

     titleString[0]="ProjectId/Institutionnbr"; 
     titleString[1]="GroupName"; 
     titleString[2]="ACL Name"; 

     titleList.add(titleString); 
     csvwriter.writeAll(titleList); 

     for(int i = 0; i <proj_list.size();i++) { 

     //grpqry = tempQuery+proj_list.get(i) + "_ed_off_grp'" ; 
     grpqry = MessageFormat.format(tempQuery,proj_list.get(i)); 

     //chkqry = queryProp.getProperty(IdocsConstants.P_GROUP_VERIFY); 

     //System.out.println(grpqry); 
     //getACLQuery = MessageFormat.format(tempQuery2, proj_list.get(i)); 

     //System.out.println(getACLQuery); 
     //System.out.println("grp_coll query is executing...."); 
     grp_coll = Util.executeQuery(sess, grpqry, IDfQuery.READ_QUERY); 
     //System.out.println("verification query is executing....."); 
     //chk_coll = Util.executeQuery(sess, chkqry, IDfQuery.READ_QUERY); 
     //acl_coll = Util.executeQuery(sess, getACLQuery, IDfQuery.READ_QUERY); 


     if (grp_coll!=null && grp_coll.next()) { 

      String grpName = grp_coll.getString("group_name"); 
      grp_list.add(grpName); 

      System.out.println("Got group for "+proj_list.get(i)+" :: "+grpName); 

      projList=new String[3]; 

      projList[0]=proj_list.get(i); 
      projList[1]=grpName; 

      //System.out.println(grpName); 
      projList_Map.put(proj_list.get(i),grp_list.get(j)); 
      j++; 
      System.out.println(projList_Map.size()); 

      if(chkqry == null){ 
       //System.out.println("group names are adding to the list....."); 
       //grp_list.add(grpName); 

       String acl_name = queryProp.getProperty(IdocsConstants.P_GETACL); 
       acl_list.add(acl_name); 
       projList[2]=acl_name; 
       } 

      csvList.add(projList); 
      csvwriter.writeAll(csvList); 
     } 


    } 
     System.out.println("Project List is loading...."); 
     Set<String> keySet = projList_Map.keySet(); 
     System.out.println(grp_list); 
     System.out.println(acl_list); 
     for(String set : keySet) { 
      System.out.println(set + " : " +projList_Map.get(set)); 
     } 



     csvwriter.close(); 

    } catch (DfException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    catch (IndexOutOfBoundsException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public static void main(String args[]) 
{ 
    ChkGroupExistence chkexist = new ChkGroupExistence(); 
} 

}

CSVWriter.java

public class CSVWriter {

/** 
* Default Delimiter will be used if not given. 
*/ 
private static final char DEF_DELIM = ','; 

/** 
* Default Quote Character will be used if not given. 
*/ 
private static final char DEF_QUOTE_CHAR = '"'; 

/** 
* Default End of Line Character. 
*/ 
private static final char DEFAULT_EOL = '\n'; 

/** 
* Contains the Delimtter. 
*/ 
private char delimiter; 

/** 
* Contains the Quote character. 
*/ 
private char quotechar; 

/** 
* String contains the End of the line character. 
*/ 
private char cLineEnd; 

/** 
* Instance Variable to hold Write Object. 
*/ 
private Writer rawWriter; 

/** 
* Instance variable to hold PrintWriter object 
*/ 
private PrintWriter pw; 

/** 
* Constructor to take File Writer as Input. 
* 
* @param writer 
*   File Writer 
*/ 
public CSVWriter(Writer writer) { 
    this(writer, DEF_DELIM); 
} 

/** 
* Constructor to take File Writer and Delimiter as Input. 
* 
* @param writer 
*   File Writer 
* @param delim 
*   Delimiter 
*/ 
public CSVWriter(Writer writer, char delim) { 
    this(writer, delim, DEF_QUOTE_CHAR); 
} 

/** 
* Constructor to take File Writer, Delimiter and Quote Character as Input. 
* 
* @param writer 
*   File Writer 
* @param delim 
*   Delimiter 
* @param quote 
*   Quote Character 
*/ 
public CSVWriter(Writer writer, char delim, char quote) { 
    this(writer, delim, quote, DEFAULT_EOL); 
} 

/** 
* Constructor to take File Writer, Delimiter, Quote Character and End of 
* Line feed as Input. 
* 
* @param writer 
*   File Writer 
* @param delim 
*   Delimiter 
* @param quote 
* @param sEOL 
*/ 
public CSVWriter(Writer writer, char delim, char quote, char sEOL) { 
    rawWriter = writer; 
    pw = new PrintWriter(writer); 
    delimiter = delim; 
    quotechar = quote; 
    cLineEnd = sEOL; 
} 

/** 
* Method takes List as input and writes values into the CSV file. 
* 
* @param list 
*   List of Cell values. 
*/ 
public void writeAll(List list) { 
    String sRow[]; 
    for (Iterator iter = list.iterator(); iter.hasNext(); writeNext(sRow)) { 
     sRow = (String[]) iter.next(); 
    } 
} 

/** 
* Method that takes String[] as input and writes each and every cell. 
* 
* @param sRow 
*   String[] 
*/ 
private void writeNext(String sRow[]) { 
    StringBuffer stringbuffer = new StringBuffer(); 
    for (int i = 0; i < sRow.length; i++) { 
     if (i != 0) { 
      stringbuffer.append(delimiter); 
     } 
     String s = sRow[i]; 
     if (s == null) { 
      continue; 
     } 
     if (quotechar != 0) { 
      stringbuffer.append(quotechar); 
     } 
     for (int j = 0; j < s.length(); j++) { 
      char c = s.charAt(j); 
      if (c == quotechar) { 
       stringbuffer.append(DEF_QUOTE_CHAR).append(c); 
       continue; 
      } 
      if (c == DEF_QUOTE_CHAR) { 
       stringbuffer.append(DEF_QUOTE_CHAR).append(c); 
      } else { 
       stringbuffer.append(c); 
      } 
     } 

     if (quotechar != 0) { 
      stringbuffer.append(quotechar); 
     } 
    } 

    stringbuffer.append(cLineEnd); 
    pw.write(stringbuffer.toString()); 
} 

/** 
* Method that closed the Print Writer. Only when this method is called, the 
* CSV file will be saved. 
* 
* @throws IOException 
*/ 
public void close() throws IOException { 
    pw.flush(); 
    pw.close(); 
    rawWriter.close(); 
} 

Antwort

1

Wahrscheinlich, weil Sie ein Wiederholungsattribut in Ihrer Abfrage einschließlich sind .

Tipp: Ich neige dazu, die Objekt-ID für Referenz und Debugging, wenn ich dies tue.

Sie können auch ein Schlüsselwort "DISTINCT" in Ihre Abfrage einfügen, wenn Sie diese sich wiederholenden Attribute einschließen. Der Grund dafür, dass sie sich so verhalten, liegt in der Art des Datenmodells: Alle sich wiederholenden Attribute werden in derselben Tabelle gespeichert. Wenn eines dieser Attribute mehrere Werte enthält, könnte eine Abfrage mehrere Zeilen zurückgeben, wenn ein anderes sich wiederholendes Attribut abgefragt wird. In diesem Fall würde eine SELECT DISTINCT den Trick machen.

Sie können auch mit dem DQL-Hinweis ENABLE (ROW_BASED) herumspielen, wenn Sie wiederkehrende Attribute mit Einzelwertattributen verknüpfen.

Glückliche Codierung!

+0

Dies ist in der Tat die richtige Antwort und sollte als solche gekennzeichnet werden. – eivamu