|
18 | 18 |
|
19 | 19 | package org.apereo.openequella.tools.toolbox;
|
20 | 20 |
|
| 21 | +import java.io.File; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
21 | 24 | import org.apache.logging.log4j.LogManager;
|
22 | 25 | import org.apache.logging.log4j.Logger;
|
23 | 26 | import org.apereo.openequella.tools.toolbox.checkFiles.CheckFilesDbHandler;
|
|
26 | 29 | import org.apereo.openequella.tools.toolbox.checkFiles.ResultsRow;
|
27 | 30 | import org.apereo.openequella.tools.toolbox.utils.CheckFilesUtils;
|
28 | 31 |
|
29 |
| -import java.io.File; |
30 |
| -import java.util.ArrayList; |
31 |
| -import java.util.List; |
32 |
| - |
33 |
| - |
34 | 32 | /**
|
35 | 33 | * Consider log4j pattern:
|
36 | 34 | *
|
37 |
| - * <Appenders> |
38 |
| - * <Appender type="Console" name="STDOUT"> |
39 |
| - * <Layout type="PatternLayout" pattern="%d %-5p [%c{1}]: %m%n"/> |
40 |
| - * </Appender> |
41 |
| - * </Appenders> |
42 |
| - * |
| 35 | + * <p><Appenders> <Appender type="Console" name="STDOUT"> <Layout type="PatternLayout" pattern="%d |
| 36 | + * %-5p [%c{1}]: %m%n"/> </Appender> </Appenders> |
43 | 37 | */
|
44 |
| - |
45 | 38 | public class CheckFilesDriver {
|
46 |
| - private static Logger LOGGER = LogManager.getLogger(CheckFilesDriver.class); |
47 |
| - |
48 |
| - public List<String> execute(String[] args) { |
49 |
| - List<String> results = new ArrayList<>(); |
50 |
| - try { |
51 |
| - |
52 |
| - if((args.length > 1) && args[1].equals("-compare")) { |
53 |
| - compareReports(args); |
54 |
| - } else if((args.length > 0) && args[0].endsWith(".properties")) { |
55 |
| - runCheck(args[0]); |
56 |
| - } else { |
57 |
| - LOGGER.error("ERROR - CheckFiles needs at least the configuration file as a parameter."); |
58 |
| - return results; |
59 |
| - } |
60 |
| - } catch (Exception e) { |
61 |
| - LOGGER.error(e.getMessage()); |
62 |
| - e.printStackTrace(); |
63 |
| - } |
64 |
| - |
| 39 | + private static Logger LOGGER = LogManager.getLogger(CheckFilesDriver.class); |
| 40 | + |
| 41 | + public List<String> execute(String[] args) { |
| 42 | + List<String> results = new ArrayList<>(); |
| 43 | + try { |
| 44 | + |
| 45 | + if ((args.length > 1) && args[1].equals("-compare")) { |
| 46 | + compareReports(args); |
| 47 | + } else if ((args.length > 0) && args[0].endsWith(".properties")) { |
| 48 | + runCheck(args[0]); |
| 49 | + } else { |
| 50 | + LOGGER.error("ERROR - CheckFiles needs at least the configuration file as a parameter."); |
65 | 51 | return results;
|
| 52 | + } |
| 53 | + } catch (Exception e) { |
| 54 | + LOGGER.error(e.getMessage()); |
| 55 | + e.printStackTrace(); |
66 | 56 | }
|
67 | 57 |
|
68 |
| - private static void compareReports(String[] args) { |
69 |
| - LOGGER.info("### Starting oEQ-Toolbox:CheckFiles Report Comparison..."); |
70 |
| - endCompare: { |
71 |
| - if(args.length != 4) { |
72 |
| - LOGGER.error("### Must invoke with two filenames/paths. Instead found [{}]: [{}]", args.length , args); |
73 |
| - break endCompare; |
74 |
| - } |
75 |
| - File r1 = new File(args[2]); |
76 |
| - File r2 = new File(args[3]); |
77 |
| - if(!r1.exists()) { |
78 |
| - LOGGER.error("### First file [{}] does not exist.", args[2]); |
79 |
| - break endCompare; |
80 |
| - } |
81 |
| - if(!r2.exists()) { |
82 |
| - LOGGER.error("### Second file [{}] does not exist.", args[3]); |
83 |
| - break endCompare; |
84 |
| - } |
85 |
| - |
86 |
| - ResultComparison cr = CheckFilesUtils.looseCompare(r1, r2); |
87 |
| - |
88 |
| - if(cr == null) { |
89 |
| - LOGGER.error("### Error comparing files."); |
90 |
| - break endCompare; |
91 |
| - } |
92 |
| - |
93 |
| - if(cr.areReportsEqual()) { |
94 |
| - LOGGER.info("Reports ARE similar (in terms of resultant attachments)!"); |
95 |
| - } else { |
96 |
| - LOGGER.info("Reports are NOT similar (in terms of resultant attachments)! Listing differences..."); |
97 |
| - LOGGER.info("Extra rows in the first report ({}):", cr.getFirstName()); |
98 |
| - for(ResultsRow rr : cr.getOnlyInFirst()) { |
99 |
| - LOGGER.info("\t- {}", rr); |
100 |
| - }LOGGER.info("Extra rows in the second report ({}):", cr.getSecondName()); |
101 |
| - for(ResultsRow rr : cr.getOnlyInSecond()) { |
102 |
| - LOGGER.info("\t- {}", rr); |
103 |
| - } |
104 |
| - } |
| 58 | + return results; |
| 59 | + } |
| 60 | + |
| 61 | + private static void compareReports(String[] args) { |
| 62 | + LOGGER.info("### Starting oEQ-Toolbox:CheckFiles Report Comparison..."); |
| 63 | + endCompare: |
| 64 | + { |
| 65 | + if (args.length != 4) { |
| 66 | + LOGGER.error( |
| 67 | + "### Must invoke with two filenames/paths. Instead found [{}]: [{}]", |
| 68 | + args.length, |
| 69 | + args); |
| 70 | + break endCompare; |
| 71 | + } |
| 72 | + File r1 = new File(args[2]); |
| 73 | + File r2 = new File(args[3]); |
| 74 | + if (!r1.exists()) { |
| 75 | + LOGGER.error("### First file [{}] does not exist.", args[2]); |
| 76 | + break endCompare; |
| 77 | + } |
| 78 | + if (!r2.exists()) { |
| 79 | + LOGGER.error("### Second file [{}] does not exist.", args[3]); |
| 80 | + break endCompare; |
| 81 | + } |
| 82 | + |
| 83 | + ResultComparison cr = CheckFilesUtils.looseCompare(r1, r2); |
| 84 | + |
| 85 | + if (cr == null) { |
| 86 | + LOGGER.error("### Error comparing files."); |
| 87 | + break endCompare; |
| 88 | + } |
| 89 | + |
| 90 | + if (cr.areReportsEqual()) { |
| 91 | + LOGGER.info("Reports ARE similar (in terms of resultant attachments)!"); |
| 92 | + } else { |
| 93 | + LOGGER.info( |
| 94 | + "Reports are NOT similar (in terms of resultant attachments)! Listing differences..."); |
| 95 | + LOGGER.info("Extra rows in the first report ({}):", cr.getFirstName()); |
| 96 | + for (ResultsRow rr : cr.getOnlyInFirst()) { |
| 97 | + LOGGER.info("\t- {}", rr); |
| 98 | + } |
| 99 | + LOGGER.info("Extra rows in the second report ({}):", cr.getSecondName()); |
| 100 | + for (ResultsRow rr : cr.getOnlyInSecond()) { |
| 101 | + LOGGER.info("\t- {}", rr); |
105 | 102 | }
|
106 |
| - LOGGER.info("### CheckFiles Report Comparison ended."); |
| 103 | + } |
107 | 104 | }
|
| 105 | + LOGGER.info("### CheckFiles Report Comparison ended."); |
| 106 | + } |
108 | 107 |
|
109 |
| - private static void runCheck(String propertiesFilenameStr) { |
110 |
| - LOGGER.info("### Starting CheckFiles report run..."); |
111 |
| - LOGGER.info("### Checking configs from {}...", propertiesFilenameStr); |
| 108 | + private static void runCheck(String propertiesFilenameStr) { |
| 109 | + LOGGER.info("### Starting CheckFiles report run..."); |
| 110 | + LOGGER.info("### Checking configs from {}...", propertiesFilenameStr); |
112 | 111 |
|
113 |
| - Config.reset(); |
114 |
| - Config.getInstance().init(propertiesFilenameStr); |
| 112 | + Config.reset(); |
| 113 | + Config.getInstance().init(propertiesFilenameStr); |
115 | 114 |
|
116 |
| - if (!setup()) { |
117 |
| - LOGGER.fatal("### Setup check failed. Exiting..."); |
118 |
| - return; |
119 |
| - } |
120 |
| - LOGGER.info("### Config check passed."); |
121 |
| - |
122 |
| - if(!run()) { |
123 |
| - LOGGER.fatal("### Something went wrong running the report. Exiting..."); |
124 |
| - ReportManager.getInstance().failFast(); |
125 |
| - return; |
126 |
| - } |
127 |
| - |
128 |
| - if(!finalizeRun()) { |
129 |
| - LOGGER.fatal("### Something went wrong finalizing the report. Exiting..."); |
130 |
| - return; |
131 |
| - } |
| 115 | + if (!setup()) { |
| 116 | + LOGGER.fatal("### Setup check failed. Exiting..."); |
| 117 | + return; |
| 118 | + } |
| 119 | + LOGGER.info("### Config check passed."); |
132 | 120 |
|
133 |
| - LOGGER.info("### CheckFiles {} report completed.", |
134 |
| - Config.get(Config.CF_MODE)); |
| 121 | + if (!run()) { |
| 122 | + LOGGER.fatal("### Something went wrong running the report. Exiting..."); |
| 123 | + ReportManager.getInstance().failFast(); |
| 124 | + return; |
135 | 125 | }
|
136 | 126 |
|
137 |
| - public static boolean setup() { |
138 |
| - return setup(false); |
| 127 | + if (!finalizeRun()) { |
| 128 | + LOGGER.fatal("### Something went wrong finalizing the report. Exiting..."); |
| 129 | + return; |
139 | 130 | }
|
140 | 131 |
|
141 |
| - public static boolean setup(boolean noFiles) { |
142 |
| - ReportManager.reset(); |
143 |
| - if(!Config.getInstance().isValidConfig()) { |
144 |
| - LOGGER.warn("Config is not valid. Failing setup."); |
145 |
| - return false; |
| 132 | + LOGGER.info("### CheckFiles {} report completed.", Config.get(Config.CF_MODE)); |
| 133 | + } |
| 134 | + |
| 135 | + public static boolean setup() { |
| 136 | + return setup(false); |
| 137 | + } |
| 138 | + |
| 139 | + public static boolean setup(boolean noFiles) { |
| 140 | + ReportManager.reset(); |
| 141 | + if (!Config.getInstance().isValidConfig()) { |
| 142 | + LOGGER.warn("Config is not valid. Failing setup."); |
| 143 | + return false; |
| 144 | + } |
| 145 | + ReportManager.getInstance().setup(noFiles); |
| 146 | + if (!ReportManager.getInstance().isValid()) { |
| 147 | + LOGGER.warn("Report Manager is not valid. Failing setup."); |
| 148 | + return false; |
| 149 | + } |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + public static boolean run() { |
| 154 | + switch (Config.getCheckFilesTypeConfig()) { |
| 155 | + case REST: |
| 156 | + { |
| 157 | + LOGGER.error("REST mode has not been reimplemented. Exiting."); |
| 158 | + // return (new AttachmentPingHandler()).execute(); |
| 159 | + return false; |
146 | 160 | }
|
147 |
| - ReportManager.getInstance().setup(noFiles); |
148 |
| - if (!ReportManager.getInstance().isValid()) { |
149 |
| - LOGGER.warn("Report Manager is not valid. Failing setup."); |
150 |
| - return false; |
| 161 | + case DB_BATCH_ITEMS_PER_ITEM_ATTS: |
| 162 | + case DB_ALL_ITEMS_ALL_ATTS: |
| 163 | + { |
| 164 | + return (new CheckFilesDbHandler()).execute(); |
151 | 165 | }
|
152 |
| - return true; |
153 |
| - } |
154 |
| - |
155 |
| - public static boolean run() { |
156 |
| - switch(Config.getCheckFilesTypeConfig()) { |
157 |
| - case REST: { |
158 |
| - LOGGER.error("REST mode has not been reimplemented. Exiting."); |
159 |
| - //return (new AttachmentPingHandler()).execute(); |
160 |
| - return false; |
161 |
| - } case DB_BATCH_ITEMS_PER_ITEM_ATTS: |
162 |
| - case DB_ALL_ITEMS_ALL_ATTS: { |
163 |
| - return (new CheckFilesDbHandler()).execute(); |
164 |
| - } default: { |
165 |
| - LOGGER.error("Unknown CheckFiles mode of [{}]...", Config.get(Config.CF_MODE)); |
166 |
| - return false; |
167 |
| - } |
| 166 | + default: |
| 167 | + { |
| 168 | + LOGGER.error("Unknown CheckFiles mode of [{}]...", Config.get(Config.CF_MODE)); |
| 169 | + return false; |
168 | 170 | }
|
169 | 171 | }
|
| 172 | + } |
170 | 173 |
|
171 |
| - public static boolean finalizeRun() { |
172 |
| - return ReportManager.getInstance().finalizeReporting(); |
173 |
| - } |
| 174 | + public static boolean finalizeRun() { |
| 175 | + return ReportManager.getInstance().finalizeReporting(); |
| 176 | + } |
174 | 177 | }
|
0 commit comments