001    package net.sf.cpsolver.studentsct;
002    
003    import java.io.BufferedReader;
004    import java.io.File;
005    import java.io.FileReader;
006    import java.io.FileWriter;
007    import java.io.PrintWriter;
008    import java.util.ArrayList;
009    import java.util.Iterator;
010    import java.util.List;
011    
012    /**
013     * Process all choice files (files choices.csv) in all subfolders of the given
014     * folder and create a CSV (comma separated values text file) combining all
015     * choices (one column for each choice file) of the found choices files.
016     * 
017     * @version StudentSct 1.2 (Student Sectioning)<br>
018     *          Copyright (C) 2007 - 2010 Tomas Muller<br>
019     *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
020     *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
021     * <br>
022     *          This library is free software; you can redistribute it and/or modify
023     *          it under the terms of the GNU Lesser General Public License as
024     *          published by the Free Software Foundation; either version 3 of the
025     *          License, or (at your option) any later version. <br>
026     * <br>
027     *          This library is distributed in the hope that it will be useful, but
028     *          WITHOUT ANY WARRANTY; without even the implied warranty of
029     *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
030     *          Lesser General Public License for more details. <br>
031     * <br>
032     *          You should have received a copy of the GNU Lesser General Public
033     *          License along with this library; if not see
034     *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
035     */
036    public class GetChoices {
037    
038        public static void getChoicesFile(File folder, List<List<String>> choices, String prefix) {
039            File choicesFile = new File(folder, "choices.csv");
040            if (choicesFile.exists()) {
041                System.out.println("Reading " + choicesFile + " ...");
042                try {
043                    List<String> prefixes = null;
044                    if (choices.isEmpty()) {
045                        prefixes = new ArrayList<String>();
046                        choices.add(prefixes);
047                    } else {
048                        prefixes = choices.get(0);
049                    }
050                    prefixes.add(prefix);
051                    BufferedReader reader = new BufferedReader(new FileReader(choicesFile));
052                    String line = null;
053                    for (int idx = 1; (line = reader.readLine()) != null; idx++) {
054                        List<String> cx = null;
055                        if (choices.size() <= idx) {
056                            cx = new ArrayList<String>();
057                            choices.add(cx);
058                        } else {
059                            cx = choices.get(idx);
060                        }
061                        cx.add(line);
062                    }
063                    reader.close();
064                } catch (Exception e) {
065                    System.err.println("Error reading file " + choicesFile + ", message: " + e.getMessage());
066                }
067            }
068        }
069    
070        public static void getChoices(File folder, List<List<String>> choices, String prefix) {
071            System.out.println("Checking " + folder + " ...");
072            File[] files = folder.listFiles();
073            getChoicesFile(folder, choices, (prefix == null ? "" : prefix));
074            for (int i = 0; i < files.length; i++)
075                if (files[i].isDirectory())
076                    getChoices(files[i], choices, (prefix == null ? "" : prefix + "/") + files[i].getName());
077        }
078    
079        public static void writeChoices(List<List<String>> choices, File file) {
080            try {
081                System.out.println("Writing " + file + " ...");
082                PrintWriter writer = new PrintWriter(new FileWriter(file, false));
083                for (List<String> cx : choices) {
084                    for (Iterator<String> f = cx.iterator(); f.hasNext();) {
085                        String s = f.next();
086                        writer.print(s);
087                        if (f.hasNext())
088                            writer.print(",");
089                    }
090                    writer.println();
091                }
092                writer.flush();
093                writer.close();
094            } catch (Exception e) {
095                System.err.println("Error writing file " + file + ", message: " + e.getMessage());
096            }
097        }
098    
099        public static void main(String[] args) {
100            try {
101                File folder = new File(args[0]);
102                List<List<String>> choices = new ArrayList<List<String>>();
103                getChoices(folder, choices, null);
104                if (!choices.isEmpty())
105                    writeChoices(choices, new File(folder, "all-choices.csv"));
106            } catch (Exception e) {
107                e.printStackTrace();
108            }
109        }
110    }