use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.
the class CommonDataProvider method getModel.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public Class<Model> getModel(String modelPackagesCsv) throws TechnicalException {
if (modelPackagesCsv != null && !"".equals(modelPackagesCsv)) {
String[] packages = modelPackagesCsv.split(";");
try {
if (packages.length > 0) {
Set<Class<?>> returnedClasses;
logger.debug("packages length is {}", packages.length);
for (String p : packages) {
returnedClasses = getClasses(p);
logger.debug("package [{}] return {} classes", p, returnedClasses.size());
for (Class<?> c : returnedClasses) {
if (Model.class.isAssignableFrom(c)) {
boolean mappingOK = false;
for (Field f : c.getDeclaredFields()) {
if (f.isAnnotationPresent(Column.class)) {
mappingOK = columns.contains(f.getAnnotation(Column.class).name());
}
}
if (mappingOK) {
return (Class<Model>) c;
}
}
}
}
}
return null;
} catch (Exception e) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_DATA_IOEXCEPTION), e);
}
} else {
return null;
}
}
use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.
the class Context method initDataId.
private static void initDataId(String scenarioName) throws TechnicalException {
final List<DataIndex> indexData = new ArrayList<>();
try {
Context.getDataInputProvider().prepare(scenarioName);
final Class<Model> model = Context.getDataInputProvider().getModel(Context.getModelPackages());
if (model != null) {
final String[] headers = Context.getDataInputProvider().readLine(0, false);
if (headers != null) {
final Constructor<Model> modelConstructor = DataUtils.getModelConstructor(model, headers);
final Map<String, ModelList> fusionedData = DataUtils.fusionProcessor(model, modelConstructor);
int dataIndex = 0;
for (final Entry<String, ModelList> e : fusionedData.entrySet()) {
dataIndex++;
indexData.add(new DataIndex(dataIndex, e.getValue().getIds()));
}
} else {
logger.error(Messages.getMessage(ScenarioInitiator.SCENARIO_INITIATOR_ERROR_EMPTY_FILE));
}
} else {
for (int i = 1; i < Context.getDataInputProvider().getNbLines(); i++) {
final List<Integer> index = new ArrayList<>();
index.add(i);
indexData.add(new DataIndex(i, index));
}
}
Context.getDataInputProvider().setIndexData(indexData);
} catch (final Exception te) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE) + te.getMessage(), te);
}
}
use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.
the class ShellCommand method run.
public int run() throws TechnicalException {
final Runtime rt = Runtime.getRuntime();
final List<String> cmdList = new ArrayList<>();
cmdList.add(command);
logger.info(Messages.getMessage(SHELL_RUNNING_COMMAND), command);
for (final String param : parameters) {
logger.info(param);
cmdList.add(param);
}
try {
final Process p = rt.exec(cmdList.toArray(new String[cmdList.size()]));
final BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
logger.info(line);
}
return p.waitFor();
} catch (IOException | InterruptedException e) {
logger.error("error ShellCommand.run()", e);
Thread.currentThread().interrupt();
throw new TechnicalException(e.getMessage(), e);
}
}
use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.
the class DataUtils method fusionProcessor.
public static Map<String, ModelList> fusionProcessor(Class<Model> model, Constructor<Model> modelConstructor) throws TechnicalException {
final Map<String, ModelList> fusionedData = new LinkedHashMap<>();
try {
final Class<? extends ModelList> modelListClass = model.newInstance().getModelList();
String[] example = Context.getDataInputProvider().readLine(1, false);
int i = 2;
do {
final String key = example[0];
final Object[] data = addStringToBeginningOfObjectArray(String.valueOf(i - 1), Arrays.copyOfRange(example, 1, example.length));
if (fusionedData.containsKey(key)) {
fusionedData.put(key, fusionedData.get(key).addModel(modelConstructor.newInstance(data)));
} else {
fusionedData.put(key, modelListClass.newInstance().addModel(modelConstructor.newInstance(data)));
}
example = Context.getDataInputProvider().readLine(i, false);
i++;
} while (example != null);
} catch (IllegalAccessException | InstantiationException | IllegalArgumentException | InvocationTargetException e) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_FUSION_PROCESSOR), e);
}
return fusionedData;
}
use of com.github.noraui.exception.TechnicalException in project NoraUi by NoraUi.
the class DBDataProvider method initColumns.
private void initColumns() throws DatabaseException, TechnicalException {
columns = new ArrayList<>();
String sqlRequest;
try {
final Path file = Paths.get(dataInPath + scenarioName + ".sql");
sqlRequest = new String(Files.readAllBytes(file), Charset.forName(Constants.DEFAULT_ENDODING));
sqlSanitized4readOnly(sqlRequest);
} catch (final IOException e) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE) + e.getMessage(), e);
}
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(sqlRequest);
ResultSet rs = statement.executeQuery()) {
if (rs.getMetaData().getColumnCount() < 1) {
throw new DatabaseException("Input data is empty. No column have been found.");
}
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
columns.add(rs.getMetaData().getColumnLabel(i));
}
} catch (final SQLException e) {
throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE) + e.getMessage(), e);
}
resultColumnName = Messages.getMessage(ResultColumnNames.RESULT_COLUMN_NAME);
}
Aggregations