Search in sources :

Example 16 with Parameter

use of org.cerberus.crud.entity.Parameter in project cerberus-source by cerberustesting.

the class ParameterDAO method readByKey.

@Override
public AnswerItem readByKey(String system, String param) {
    AnswerItem<Parameter> ans = new AnswerItem<>();
    MessageEvent msg = null;
    try (Connection connection = databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(Query.READ_BY_KEY)) {
        // Prepare and execute query
        preStat.setString(1, system);
        preStat.setString(2, param);
        try (ResultSet resultSet = preStat.executeQuery()) {
            while (resultSet.next()) {
                ans.setItem(loadFromResultSet(resultSet));
            }
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("ITEM", OBJECT_NAME).resolveDescription("OPERATION", "SELECT");
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        }
    } catch (Exception e) {
        LOG.warn("Unable to execute query : " + e.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
    } finally {
        // We always set the result message
        ans.setResultMessage(msg);
    }
    return ans;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Parameter(org.cerberus.crud.entity.Parameter) IFactoryParameter(org.cerberus.crud.factory.IFactoryParameter) FactoryParameter(org.cerberus.crud.factory.impl.FactoryParameter) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem) SQLException(java.sql.SQLException) CerberusException(org.cerberus.exception.CerberusException)

Example 17 with Parameter

use of org.cerberus.crud.entity.Parameter in project cerberus-source by cerberustesting.

the class ApplicationObjectDAO method readImageByKey.

@Override
public BufferedImage readImageByKey(String application, String object) {
    BufferedImage image = null;
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", "cerberus_applicationobject_path Parameter not found");
    AnswerItem a = parameterService.readByKey("", "cerberus_applicationobject_path");
    if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        Parameter p = (Parameter) a.getItem();
        String uploadPath = p.getValue();
        a = readByKey(application, object);
        if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            ApplicationObject ao = (ApplicationObject) a.getItem();
            if (ao != null) {
                File picture = new File(uploadPath + File.separator + ao.getID() + File.separator + ao.getScreenShotFileName());
                try {
                    image = ImageIO.read(picture);
                } catch (IOException e) {
                    LOG.warn("Impossible to read the image");
                }
            }
        } else {
            LOG.warn("Application Object not found");
        }
    } else {
        LOG.warn("cerberus_applicationobject_path Parameter not found");
    }
    a.setResultMessage(msg);
    return image;
}
Also used : MessageEvent(org.cerberus.engine.entity.MessageEvent) IFactoryApplicationObject(org.cerberus.crud.factory.IFactoryApplicationObject) ApplicationObject(org.cerberus.crud.entity.ApplicationObject) Parameter(org.cerberus.crud.entity.Parameter) IOException(java.io.IOException) AnswerItem(org.cerberus.util.answer.AnswerItem) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 18 with Parameter

use of org.cerberus.crud.entity.Parameter in project cerberus-source by cerberustesting.

the class ApplicationObjectDAO method uploadFile.

@Override
public Answer uploadFile(int id, FileItem file) {
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", "cerberus_applicationobject_path Parameter not found");
    AnswerItem a = parameterService.readByKey("", "cerberus_applicationobject_path");
    if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        Parameter p = (Parameter) a.getItem();
        String uploadPath = p.getValue();
        File appDir = new File(uploadPath + File.separator + id);
        if (!appDir.exists()) {
            try {
                appDir.mkdirs();
            } catch (SecurityException se) {
                LOG.warn("Unable to create application dir: " + se.getMessage());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", se.toString());
                a.setResultMessage(msg);
            }
        }
        if (a.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            deleteFolder(appDir, false);
            File picture = new File(uploadPath + File.separator + id + File.separator + file.getName());
            try {
                file.write(picture);
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("DESCRIPTION", "Application Object file uploaded");
                msg.setDescription(msg.getDescription().replace("%ITEM%", "Application Object").replace("%OPERATION%", "Upload"));
            } catch (Exception e) {
                LOG.warn("Unable to upload application object file: " + e.getMessage());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
            }
        }
    } else {
        LOG.warn("cerberus_applicationobject_path Parameter not found");
    }
    a.setResultMessage(msg);
    return a;
}
Also used : MessageEvent(org.cerberus.engine.entity.MessageEvent) Parameter(org.cerberus.crud.entity.Parameter) AnswerItem(org.cerberus.util.answer.AnswerItem) File(java.io.File) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 19 with Parameter

use of org.cerberus.crud.entity.Parameter in project cerberus-source by cerberustesting.

the class ParameterService method findParameterByKey.

@Override
public Parameter findParameterByKey(String key, String system) throws CerberusException {
    Parameter myParameter;
    /**
     * We try to get the parameter using the system parameter but if it does
     * not exist or empty, we get it with system="" which correspond to the
     * default global Cerberus Parameter.
     */
    try {
        LOG.debug("Trying to retrieve parameter : " + key + " - [" + system + "]");
        myParameter = parameterDao.findParameterByKey(system, key);
        if (myParameter != null && myParameter.getValue().equalsIgnoreCase("")) {
            myParameter = parameterDao.findParameterByKey("", key);
        }
    } catch (CerberusException ex) {
        LOG.debug("Trying to retrieve parameter (default value) : " + key + " - []");
        myParameter = parameterDao.findParameterByKey("", key);
        return myParameter;
    }
    return myParameter;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) Parameter(org.cerberus.crud.entity.Parameter)

Example 20 with Parameter

use of org.cerberus.crud.entity.Parameter in project cerberus-source by cerberustesting.

the class ParameterService method getParameterLongByKey.

@Override
public long getParameterLongByKey(String key, String system, long defaultValue) {
    Parameter myParameter;
    long outPutResult = defaultValue;
    try {
        myParameter = this.findParameterByKey(key, system);
        outPutResult = Long.parseLong(myParameter.getValue());
    } catch (CerberusException | NumberFormatException ex) {
        LOG.error("Error when trying to retreive parameter : '" + key + "' for system : '" + system + "'. Default value returned : '" + defaultValue + "'. Trace : " + ex);
    }
    LOG.debug("Success loading parameter : '" + key + "' for system : '" + system + "'. Value returned : '" + outPutResult + "'");
    return outPutResult;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) Parameter(org.cerberus.crud.entity.Parameter)

Aggregations

Parameter (org.cerberus.crud.entity.Parameter)22 CerberusException (org.cerberus.exception.CerberusException)12 MessageEvent (org.cerberus.engine.entity.MessageEvent)9 SQLException (java.sql.SQLException)8 AnswerItem (org.cerberus.util.answer.AnswerItem)8 IFactoryParameter (org.cerberus.crud.factory.IFactoryParameter)7 FactoryParameter (org.cerberus.crud.factory.impl.FactoryParameter)7 Connection (java.sql.Connection)6 PreparedStatement (java.sql.PreparedStatement)6 ResultSet (java.sql.ResultSet)6 IParameterService (org.cerberus.crud.service.IParameterService)4 JSONObject (org.json.JSONObject)4 File (java.io.File)3 MessageGeneral (org.cerberus.engine.entity.MessageGeneral)3 AnswerList (org.cerberus.util.answer.AnswerList)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 ParameterService (org.cerberus.crud.service.impl.ParameterService)2