Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.azirar.requester.ws.utils; import java.util.LinkedList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.azirar.requester.commons.ConfigurationIO; import com.azirar.requester.commons.PropertiesReader; import com.azirar.requester.core.Column; import com.azirar.requester.core.Configuration; import com.azirar.requester.core.Configurations; import com.azirar.requester.core.Constants; import com.azirar.requester.core.RequesterException; import com.azirar.requester.core.Service; import com.azirar.requester.core.Translation; import com.azirar.requester.core.TranslationValue; import com.azirar.requester.core.TranslationValues; import com.azirar.requester.core.rules.BaseRules; import com.azirar.requester.data.entity.Injection; import com.azirar.requester.data.entity.ItemValue; import com.azirar.requester.data.entity.Request; import com.azirar.requester.data.entity.Result; import com.azirar.requester.data.entity.Row; /** * The Class RequesterUtils. * * @author mazirar */ @Component public class RequesterUtils { /** The properties reader. */ @Autowired private PropertiesReader propertiesReader; /** The configurations. */ private static List<Configuration> configurations = new LinkedList<Configuration>(); /** * Gets the all configurations. * * @return the all configurations * @throws RequesterException */ public List<Configuration> getAllConfigurations() throws RequesterException { initConfigurations(); return configurations; } /** * Gets the configuration by id. * * @param pIdConfiguration * the id configuration * @return the configuration by id * @throws RequesterException */ public Configuration getConfiguration(String pIdConfiguration) throws RequesterException { initConfigurations(); if (configurations != null) { for (Configuration vConfiguration : configurations) { if (vConfiguration.idConfiguration != null) { if (vConfiguration.idConfiguration.equals(pIdConfiguration)) { return vConfiguration; } } } } Logger.getLogger(RequesterUtils.class.getName()).log(Level.SEVERE, Constants.ERROR_NO_CONFIGURATION_FOUNDED); throw new RequesterException(Constants.ERROR_NO_CONFIGURATION_FOUNDED); } /** * Gets the all services by id configuration. * * @param pIdConfiguration * the id configuration * @return the all services by id configuration * @throws RequesterException */ public List<Service> getAllServicesByIdConfiguration(String pIdConfiguration) throws RequesterException { initConfigurations(); Configuration conf = getConfiguration(pIdConfiguration); if (conf != null) { return conf.services; } Logger.getLogger(RequesterUtils.class.getName()).log(Level.SEVERE, Constants.ERROR_NO_SERVICES_FOUNDED); throw new RequesterException(Constants.ERROR_NO_SERVICES_FOUNDED); } /** * Gets the service by id. * * @param pIdService * the id service * @param version * @return the service by id * @throws RequesterException */ public Service getService(String pIdService, String pIdConfiguration) throws RequesterException { initConfigurations(); Configuration vConfiguration = getConfiguration(pIdConfiguration); if (vConfiguration.idConfiguration != null) { for (Service service : vConfiguration.services) { if (service.idService.equals(pIdService)) { return service; } } } Logger.getLogger(RequesterUtils.class.getName()).log(Level.SEVERE, Constants.ERROR_NO_SERVICE_FOUNDED); throw new RequesterException(Constants.ERROR_NO_SERVICE_FOUNDED); } /** * Gets the all columns by id services. * * @param pIdService * the id service * @return the all columns by id services * @throws RequesterException */ public List<Column> getAllColumnsByIdService(String pIdService, String pIdConfiguration) throws RequesterException { Service service = getService(pIdService, pIdConfiguration); List<Column> columns = service.columns; if (columns == null) { Logger.getLogger(RequesterUtils.class.getName()).log(Level.SEVERE, Constants.ERROR_NO_COLUMN_FOUNDED); throw new RequesterException(Constants.ERROR_NO_COLUMN_FOUNDED); } return columns; } /** * Gets the columns by id. * * @param pColumnName * the id column * @param version * @param idService * @return the columns by id * @throws RequesterException */ public Column getColumn(String idColumn, String idService, String pIdConfiguration) throws RequesterException { initConfigurations(); Service service = this.getService(idService, pIdConfiguration); if (service != null) { for (Column column : service.columns) { if (column.idColumn.equals(idColumn)) { return column; } } } Logger.getLogger(RequesterUtils.class.getName()).log(Level.SEVERE, Constants.ERROR_NO_COLUMN_FOUNDED); throw new RequesterException(Constants.ERROR_NO_COLUMN_FOUNDED); } /** * Inits the configurations. * * @throws RequesterException */ private void initConfigurations() throws RequesterException { if (configurations == null || configurations.isEmpty()) { configurations = new LinkedList<Configuration>(); try { Logger.getLogger(RequesterUtils.class.getName()) .warning("\n\n -------------> " + propertiesReader.URL_XML_CONFS); String[] uriConfs = propertiesReader.URL_XML_CONFS.split(","); for (String uriConf : uriConfs) { Logger.getLogger(RequesterUtils.class.getName()) .warning("\n\n READ CONFIGURATION-------------> " + uriConf); configurations.addAll(ConfigurationIO.read(Configurations.class, uriConf).configurations); Logger.getLogger(RequesterUtils.class.getName()) .warning("\n\n READ CONFIGURATION-------------> " + uriConf + " SUCCES"); } if (configurations != null && configurations.size() > 0) { for (Configuration conf : configurations) { if (conf.translations != null && conf.translations.size() > 0) { for (Translation translation : conf.translations) { TranslationValues translationValues = ConfigurationIO.read(TranslationValues.class, translation.jsonUrl); if (translationValues != null && translationValues.translationValues != null && translationValues.translationValues.size() > 0) { for (TranslationValue translationValue : translationValues.translationValues) { translation.translationValues.put(translationValue.key.toUpperCase(), translationValue.value); conf.languages.put(translation.language.toUpperCase(), translation); } } } } } } BaseRules.verifyConfigurations(configurations); } catch (Exception ex) { Logger.getLogger(RequesterUtils.class.getName()).log(Level.SEVERE, Constants.ERROR_CONFIGURATIOIN_FILE); throw new RequesterException(Constants.ERROR_CONFIGURATIOIN_FILE); } } } public JSONObject getJson(Result result, Request request) throws RequesterException { if (result != null && !result.getRows().isEmpty()) { return buildJson(request, result, result.getRows().get(0)); } return null; } public List<JSONObject> getListJson(Result result, Request request) throws RequesterException { List<JSONObject> jsonObjects = new LinkedList<JSONObject>(); if (result != null && !result.getRows().isEmpty()) { for (Row row : result.getRows()) { jsonObjects.add(buildJson(request, result, row)); } } return jsonObjects; } private JSONObject buildJson(Request request, Result result, Row row) throws RequesterException { JSONObject jsonObject = new JSONObject(); Service service = getService(request.getIdService(), request.getIdConfiguration()); Configuration configuration = getConfiguration(request.getIdConfiguration()); if (service.isGetOperation()) for (ItemValue item : row.getItemValues()) { String translatedName = configuration.translate(request.getLanguage().trim(), item.getName()); if (item.getValue() != null) { Column vColumn = getColumnByName(item.getName(), request.getIdService(), request.getIdConfiguration()); if (vColumn.isText()) { jsonObject.put(translatedName, item.getValue()); } if (vColumn.isNumber() || vColumn.isDate()) { jsonObject.put(translatedName, Long.parseLong(item.getValue())); } if (vColumn.isFoat()) { jsonObject.put(translatedName, Long.parseLong(item.getValue())); } if (vColumn.isBoolean()) { jsonObject.put(translatedName, Boolean.parseBoolean(item.getValue())); } } else { jsonObject.put(translatedName, JSONObject.NULL); } } else jsonObject.put(row.getItemValues().get(0).getName(), row.getItemValues().get(0).getValue()); return jsonObject; } public Column getColumnByName(String vColumnName, String idService, String idConfiguration) throws RequesterException { initConfigurations(); Service service = this.getService(idService, idConfiguration); if (service != null) { for (Column column : service.columns) { if (column.name.toUpperCase().equals(vColumnName.toUpperCase())) { return column; } } } Logger.getLogger(RequesterUtils.class.getName()).log(Level.SEVERE, Constants.ERROR_NO_COLUMN_FOUNDED); throw new RequesterException(Constants.ERROR_NO_COLUMN_FOUNDED); } public String getXMLFromConfigurations() throws RequesterException { String confsXML = "" // + "<?xml version=\"1.0\" encoding=\"UTF-8\" // standalone=\"true\"?>\n" + "<configurations>\n"; initConfigurations(); if (configurations != null && !configurations.isEmpty()) { for (Configuration configuration : configurations) { confsXML += configuration.getXML(); } } confsXML += "</configurations>\n"; return confsXML; } public String getHTMLFromConfigurations() throws RequesterException { String confsHTML = "" + "<html>" + "<Head>" + "<title>RequesterWS</title>" + "<style> \n" + "" + "ol {counter-reset: repas; } /* on initialise et nomme un compteur */ \n" + "li { \n" + " list-style-type: none; \n" + " counter-increment: repas; /* on incrmente le compteur chaque nouveau li */ \n" + " margin-bottom: 10px; \n" + "} \n" + "li:before { \n" + " content: \"\"; /* on affiche le compteur */ \n" + " padding: 0 20px 6px; \n" + " margin-right: 8px; \n" + " vertical-align: top; \n" + " background: #678; \n" + " -moz-border-radius: 60px; \n" + " border-radius: 60px; \n" + " font-weight: bold; \n" + " font-size: 0.8em; \n" + " color: white; \n" + " } \n" + " " + "</style> " + " " + " " + " \n\n" + " <script type='text/javascript'> \n" + " function hideShowElement(elementId) {\n" + " var element = document.getElementById(elementId); \n" + " if(element.style.display === 'none'){\n" + " element.style.display = 'block';\n" + " } else {\n" + " element.style.display = 'none';\n" + " }\n" + " \n" + "}\n\n" + "</script>" + "</Head>" + "<body>\n"; initConfigurations(); if (configurations != null && !configurations.isEmpty()) { for (Configuration configuration : configurations) { confsHTML += "<div><h2><font size='4' color='blue'>" + configuration.name + "</font></h2></div>"; confsHTML += "<div id='Id_" + configuration.idConfiguration + "'>" + "<font size='4'>"; confsHTML += configuration.getHTML(configuration); confsHTML += "</font><div>"; } } return confsHTML; } /** * Check parameters. * * @param request * the parameters * @throws RequesterException * @throws Exception * the exception */ public static void checkParameters(Request request) throws RequesterException { if (request == null || ((request.getIdService() == null || request.getIdConfiguration() == null) && request.getIdRequest() == null)) { throw new RequesterException(Constants.ERROR_REQUEST_PARAMETERS); } } public static void checkInjections(Request request) throws RequesterException { if (request != null && request.getInjection() != null) { if (request.getSelectedFields() == null || request.getSelectedFields().split(";").length <= 0) { throw new RequesterException(Constants.ERROR_REQUEST_INJECTIONS_NO_SELECTED_FIELDS_TO_INJECT); } if (request.getInjection().getFields() == null || request.getInjection().getFields().split(";").length <= 0) { throw new RequesterException(Constants.ERROR_REQUEST_INJECTIONS_NO_FIELDS_TO_INJECT); } } } }