/* * Copyright (c) 2014 SIEMENS AUSTRIA. All Rights Reserved. * * This software is the confidential and proprietary information of SIEMENS. You * shall disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into with * SIEMENS. * * Information in this document is subject to change without notice. */ package com.siemens.cora.mmi.util; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.sql.Date; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.HashSet; import java.util.InvalidPropertiesFormatException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Properties; import java.util.Set; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; import com.reuters.rfa.common.Client; import com.reuters.rfa.common.Context; import com.reuters.rfa.common.DispatchException; import com.reuters.rfa.common.Event; import com.reuters.rfa.common.EventQueue; import com.reuters.rfa.common.Handle; import com.reuters.rfa.common.QualityOfServiceRequest; import com.reuters.rfa.common.StandardPrincipalIdentity; import com.reuters.rfa.common.TokenizedPrincipalIdentity; import com.reuters.rfa.session.event.ConnectionStatus; import com.reuters.rfa.session.MarketDataItemSub; import com.reuters.rfa.session.MarketDataSubscriber; import com.reuters.rfa.session.MarketDataSubscriberInterestSpec; import com.reuters.rfa.session.Session; import com.reuters.rfa.session.event.ConnectionEvent; import com.reuters.rfa.session.event.EntitlementsAuthenticationEvent; import com.reuters.rfa.session.event.MarketDataItemEvent; import com.reuters.rfa.session.event.MarketDataSvcEvent; import com.reuters.rfa.session.event.MarketDataItemStatus; import com.reuters.rfa.utility.HexDump; import com.reuters.tibmsg.TibException; import com.reuters.tibmsg.TibField; import com.reuters.tibmsg.TibMsg; import com.siemens.cora.common.constants.StringConstants; import com.siemens.cora.common.exceptions.MessageKeyException; import com.siemens.cora.common.exceptions.MessagesException; import com.siemens.cora.common.gui.ErrorTextHelper; import com.siemens.cora.common.gui.Expression; import com.siemens.cora.common.gui.PropertiesLoader; import com.siemens.cora.common.text.MessageSource; import com.siemens.cora.common.utils.DateHelper; import com.siemens.cora.email.BasicEmail; import com.siemens.cora.masterdata.domain.MmiChart; import com.siemens.cora.masterdata.domain.MmiChartData; import com.siemens.cora.masterdata.service.api.MmiChartDataService; import com.siemens.cora.masterdata.service.api.MmiChartService; public class RmmicBean extends QuartzJobBean { private static Logger LOGGER = Logger.getLogger(RmmicBean.class); private static final String ERROR_TAG = ""; // RFA Properties private static final String RMTS_USER = "cora.rmts.user"; private static final String SERVICE_NAME = "cora.rmts.serviceName"; private static final String SESSION_NAME = "cora.rmts.sessionName"; private static final String RUN_TIME = "cora.rmts.runTime"; private static final String APPLICATION = "cora.rmts.application"; private static final String POSITION = "cora.rmts.position"; private static final String BEST_RATE = "cora.rmts.bestRate"; private static final String BEST_TIMELINESS = "cora.rmts.bestTimeliness"; private static final String WORST_RATE = "cora.rmts.worstRate"; private static final String WORST_TIMELINESS = "cora.rmts.worstTimeliness"; private static final String MOUNT_TPI = "cora.rmts.mounttpi"; private static final String DYNAMIC_STREAM = "cora.rmts.dynamicStream"; private static final String ENCODING_UTF8 = "UTF-8"; // Services private MmiChartService mmiChartService; private MmiChartDataService mmiChartDataService; private MessageSource messages; // RTMS variables protected boolean dynamicStream = false; protected int bestRate = 0; protected int bestTimeliness = 2147483647; protected int worstRate = 0; protected int worstTimeliness = 2147483647; protected boolean mounttpi = true; protected String rmtsUser; protected String serviceName; protected String sessionName; protected String application; protected String position; protected int runTime; // RFA objects protected StandardPrincipalIdentity _standardPI; protected TokenizedPrincipalIdentity _tokenizedPI; protected Session _session; protected EventQueue _eventQueue; protected MarketDataSubscriber _marketDataSubscriber; protected Handle _mdsClientHandle; protected Handle _itemHandle; // Other objects protected static String _date = ""; static LinkedList _records; private String mmiExportDir; // Error messages private List errorMessages = new ArrayList(); public RmmicBean() { } public MmiChartService getMmiChartService() { return mmiChartService; } public void setMmiChartService(MmiChartService mmiChartService) { this.mmiChartService = mmiChartService; } public MmiChartDataService getMmiChartDataService() { return mmiChartDataService; } public void setMmiChartDataService(MmiChartDataService mmiChartDataService) { this.mmiChartDataService = mmiChartDataService; } public MessageSource getMessages() { return messages; } public void setMessages(MessageSource messages) { this.messages = messages; } public String getMmiExportDir() { return mmiExportDir; } public void setMmiExportDir(String mmiExportDir) { LOGGER.info("setter - setMmiExportDir: " + mmiExportDir); this.mmiExportDir = mmiExportDir; } public void clearErrorMessages() { errorMessages = new ArrayList(); } public void exceptionOccured(String e) { if (errorMessages.contains(e)) { return; } errorMessages.add(e); } public void init() throws MessageKeyException { clearErrorMessages(); _date = DateHelper.getSimpleDateString(new GregorianCalendar()); _records = new LinkedList(); loadMmiProperties(); createPrincipals(); boolean initialized = Context.initialize(); if (!initialized) { LOGGER.error("Context not initialized"); throw new MessageKeyException("mmi.error.rmmic.noConnectionEstablished"); } // Create a Session if (mounttpi) { _session = Session.acquire(sessionName, _tokenizedPI); } else { _session = Session.acquire(sessionName); } if (_session == null) { LOGGER.error("Could not acquire session"); throw new MessageKeyException("mmi.error.rmmic.noConnectionEstablished"); } // Create an object to receive event callbacks MyClient myClient = new MyClient(); // Create an Event Queue _eventQueue = EventQueue.create("myEventQueue"); // Create a MarketDataSubscriber event source _marketDataSubscriber = _session.createMarketDataSubscriber("myMarketDataSubscriber", true, _standardPI); try { // Register for service and connection status MarketDataSubscriberInterestSpec marketDataSubscriberInterestSpec = new MarketDataSubscriberInterestSpec(); marketDataSubscriberInterestSpec.setMarketDataSvcInterest(true); marketDataSubscriberInterestSpec.setConnectionInterest(true); marketDataSubscriberInterestSpec.setEntitlementsInterest(true); _mdsClientHandle = _marketDataSubscriber.registerClient(_eventQueue, marketDataSubscriberInterestSpec, myClient, null); } catch (IllegalArgumentException iae) { LOGGER.error("Could not register client: ", iae); throw new MessageKeyException("mmi.error.rmmic.noConnectionEstablished"); } // Set the QOS request information QualityOfServiceRequest qosr = new QualityOfServiceRequest(); qosr.setBestRate(bestRate); qosr.setBestTimeliness(bestTimeliness); qosr.setWorstRate(worstRate); qosr.setWorstTimeliness(worstTimeliness); if (dynamicStream) { qosr.setStreamProperty(QualityOfServiceRequest.DYNAMIC_STREAM); } //else use static stream (default) try { // Get RICs LinkedList _itemNames = new LinkedList(); _itemNames.addAll(mmiChartService.getRics()); Iterator iter = _itemNames.iterator(); while (iter.hasNext()) { String itemName = iter.next(); LOGGER.info("Subscribing to " + itemName); MarketDataItemSub marketDataItemSub = new MarketDataItemSub(); marketDataItemSub.setItemName(itemName); marketDataItemSub.setServiceName(serviceName); marketDataItemSub.setRequestedQualityOfService(qosr); _marketDataSubscriber.subscribe(_eventQueue, marketDataItemSub, myClient, null); } } catch (IllegalArgumentException iae) { LOGGER.error("Could not subscribe RIC: ", iae); throw new MessageKeyException("mmi.error.rmmic.noConnectionEstablished"); } } private void loadMmiProperties() throws MessageKeyException { try { Properties mmiConfig = PropertiesLoader.getInstance() .getProperties(StringConstants.MMI_PROPERTIES); serviceName = mmiConfig.getProperty(SERVICE_NAME); sessionName = mmiConfig.getProperty(SESSION_NAME); rmtsUser = mmiConfig.getProperty(RMTS_USER); runTime = Integer.parseInt(mmiConfig.getProperty(RUN_TIME)); application = mmiConfig.getProperty(APPLICATION); position = mmiConfig.getProperty(POSITION); dynamicStream = Boolean.parseBoolean(mmiConfig.getProperty(DYNAMIC_STREAM)); bestRate = Integer.parseInt(mmiConfig.getProperty(BEST_RATE)); bestTimeliness = Integer.parseInt(mmiConfig.getProperty(BEST_TIMELINESS)); worstRate = Integer.valueOf(mmiConfig.getProperty(WORST_RATE)); worstTimeliness = Integer.parseInt(mmiConfig.getProperty(WORST_TIMELINESS)); mounttpi = Boolean.parseBoolean(mmiConfig.getProperty(MOUNT_TPI)); } catch (NullPointerException e) { LOGGER.error("Unexpected error while loading properties: ", e); throw new MessageKeyException("mmi.error.rmmic.rmtsConfig"); } catch (InvalidPropertiesFormatException e) { LOGGER.error("Unexpected error while loading properties: ", e); throw new MessageKeyException("mmi.error.rmmic.rmtsConfig"); } catch (IOException e) { LOGGER.error("Unexpected error while loading properties: ", e); throw new MessageKeyException("mmi.error.rmmic.rmtsConfig"); } catch (NumberFormatException e) { LOGGER.error("Parsing error while loading properties: ", e); throw new MessageKeyException("mmi.error.rmmic.rmtsConfig"); } } public void createPrincipals() { _tokenizedPI = new TokenizedPrincipalIdentity(); String tokenString = new String(rmtsUser); if (!application.equals("")) { tokenString = tokenString + "+" + application; if (!position.equals("")) { tokenString = tokenString + "+" + position; } } _tokenizedPI.setBuffer(tokenString.getBytes()); _standardPI = new StandardPrincipalIdentity(); _standardPI.setName(rmtsUser); _standardPI.setPosition(position); _standardPI.setAppName(application); } public void run() { // Dispatch item events for a while long startTime = System.currentTimeMillis(); while ((System.currentTimeMillis() - startTime) < (runTime * 1000)) { try { _eventQueue.dispatch(1000); } catch (DispatchException de) { LOGGER.info("Queue deactivated"); return; } } LOGGER.info(Context.string()); LOGGER.info(runTime + " seconds elapsed, " + getClass().toString() + " exiting"); } public void cleanup() { _marketDataSubscriber.unsubscribeAll(); _marketDataSubscriber.unregisterClient(_mdsClientHandle); _marketDataSubscriber.destroy(); _eventQueue.deactivate(); _session.release(); Context.uninitialize(); } public void processChartDataStream(InputStream is, boolean save) throws IOException, MessageKeyException { BufferedReader br = new BufferedReader(new InputStreamReader(is,"ISO-8859-1")); String line; Set mmiChartDataList = new HashSet(); while ((line = br.readLine()) != null) { LOGGER.info("processing line:" + line); // Skip empty lines if (line == null || line.trim().isEmpty()) { continue; } String[] lineArray = line.split(";"); if (lineArray == null || lineArray.length < 3 || lineArray[0] == null || lineArray[1] == null || lineArray[2] == null) { throw new MessageKeyException("mmi.error.import.csv.contentWrong"); } lineArray[0] = lineArray[0].trim().replaceAll("\"", ""); lineArray[1] = lineArray[1].trim().replaceAll("\"", ""); lineArray[2] = lineArray[2].trim().replaceAll("\"", ""); if (lineArray[0].isEmpty() || lineArray[1].length() != 8 || lineArray[2].isEmpty()) { throw new MessageKeyException("mmi.error.import.csv.contentWrong"); } Date chartDate; try { chartDate = Date.valueOf(lineArray[1].substring(0, 4) + "-" + lineArray[1].substring(4, 6) + "-" + lineArray[1].substring(6, 8)); } catch (IllegalArgumentException e) { LOGGER.error("Date wrong:" + lineArray[1], e); throw new MessageKeyException("mmi.error.import.csv.contentWrong"); } Integer mmiId = Integer.parseInt(lineArray[0]); Timestamp actTime = new Timestamp(System.currentTimeMillis()); Double chartValue = getChartValue(lineArray[2]); MmiChartData mmiChartData = mmiChartDataService.getMmiChartDataByMmiIdAndDate(mmiId, chartDate); if (save) { if (mmiChartData == null) { if (Double.valueOf(0).compareTo(chartValue) != 0) { mmiChartData = new MmiChartData(mmiId, DateHelper.getSimpleDateString(chartDate), chartValue); mmiChartData.setCreateDate(actTime); mmiChartData.setModifyDate(actTime); mmiChartDataList.add(mmiChartData); mmiChartDataService.create(mmiChartData); } } else if (Double.valueOf(0).compareTo(chartValue) != 0) { mmiChartData.setChartValue(chartValue); mmiChartData.setModifyDate(actTime); mmiChartDataList.add(mmiChartData); mmiChartDataService.update(mmiChartData); } else { mmiChartDataService.delete(mmiChartData); } } } if (save) { for (MmiChartData mmiChartData : mmiChartDataList) { processDependentValues(mmiChartData); } } } private Double getChartValue(String input) throws MessageKeyException { try { int firstComma = input.indexOf(","); int lastComma = input.lastIndexOf(","); int firstPoint = input.indexOf("."); int lastPoint = input.lastIndexOf("."); if (firstComma != -1 && firstPoint != -1) { if (firstComma > firstPoint) { input = input.replaceAll("\\.", ""); } else { input = input.replaceAll(",", ""); } } if (firstComma != -1 && lastComma != -1 && firstComma != lastComma) { input = input.replaceAll(",", ""); } if (firstPoint != -1 && lastPoint != -1 && firstPoint != lastPoint) { input = input.replaceAll("\\.", ""); } input = input.replaceAll(",", "."); return Double.valueOf(input); } catch (NumberFormatException e) { LOGGER.error("Number wrong: " + input, e); throw new MessageKeyException("mmi.error.import.csv.contentWrong"); } } private void processDependentValues(MmiChartData mmiChartData) { List mmiCHartFormulaList = mmiChartService.getByFormulaDependents(mmiChartData.getMmiId()); for (MmiChart mmiChart : mmiCHartFormulaList) { String formula = mmiChart.getFormula().replaceAll(" ", ""); LOGGER.info(mmiChart.getId() + " formula " + formula); String[] formulaParts = formula.split("[\\*\\/\\+\\-\\(\\)]"); for (String formulaIdStr : formulaParts) { if (formulaIdStr.startsWith("$")) { String x = formulaIdStr.replaceAll("\\$", ""); x = x.replaceAll("\\{", ""); x = x.replaceAll("\\}", ""); String formulaId = x; MmiChartData chartData = mmiChartDataService.getMmiChartDataByMmiIdAndDate(Integer.parseInt(formulaId), Date.valueOf(mmiChartData.getDateString().substring(0, 4) + "-" + mmiChartData.getDateString().substring(4, 6) + "-" + mmiChartData.getDateString().substring(6, 8))); if (chartData != null) { formula = formula.replaceAll("\\$\\{" + formulaId + "\\}", chartData.getChartValue().toString()); } else { formula = formula.replaceAll(formulaId, "NotFound"); } } } String x = formula.replaceAll("\\$", ""); x = x.replaceAll("\\{", ""); x = x.replaceAll("\\}", ""); formula = x; Timestamp actTime = new Timestamp(System.currentTimeMillis()); if (formula.indexOf("NotFound") == -1) { Expression formulaExpr = new Expression(formula); BigDecimal res = formulaExpr.eval(); String resString = res.toPlainString(); LOGGER.info(formula + " -> " + resString); MmiChartData formulaMmiChartData = mmiChartDataService.getMmiChartDataByMmiIdAndDate(mmiChart.getId(), mmiChartData.getChartDate()); if (formulaMmiChartData == null) { if (Double.valueOf(0).compareTo(Double.valueOf(resString)) != 0) { formulaMmiChartData = new MmiChartData(mmiChart.getId(), DateHelper.getSimpleDateString(mmiChartData.getChartDate()), Double.valueOf(resString)); formulaMmiChartData.setCreateDate(actTime); formulaMmiChartData.setModifyDate(actTime); mmiChartDataService.create(formulaMmiChartData); } } else if (Double.valueOf(0).compareTo(Double.valueOf(resString)) != 0){ formulaMmiChartData.setChartValue(Double.valueOf(resString)); formulaMmiChartData.setModifyDate(actTime); mmiChartDataService.update(formulaMmiChartData); } else { mmiChartDataService.delete(formulaMmiChartData); } } } } private void proccessDatabaseActions() { ListIterator i = _records.listIterator(); StringBuffer file = new StringBuffer(); InputStream in; while (i != null && i.hasNext()) { DBRecord rec = (DBRecord)i.next(); file.append(rec.getId()).append(";").append(rec.getDate()).append(";").append(rec.getSum()).append("\n"); } try { in = new ByteArrayInputStream(file.toString().getBytes(ENCODING_UTF8)); processChartDataStream(in, true); } catch (UnsupportedEncodingException e) { LOGGER.error("Unsupported encoding exception occured:", e); } catch (IOException e) { LOGGER.error("Unexpected exception occured:", e); } catch (MessageKeyException e) { LOGGER.error(getMessages().getMessage(e.getMessage(), e.getParams())); } } private String getTodaysDataFromDB() { StringBuffer chart = new StringBuffer(); List mmiChartList = mmiChartService.getAll(); Calendar date = new GregorianCalendar(); List mmiChartDataList = mmiChartDataService.getMmiChartDataByDate(new Date(date.getTimeInMillis())); Map mmiChartDataMap = new HashMap(); for (MmiChartData mmiChartData : mmiChartDataList) { mmiChartDataMap.put(mmiChartData.getMmiId(), mmiChartData.getChartValue()); } for (MmiChart mmiChart : mmiChartList) { if (mmiChart.getVisible()) { chart.append(mmiChart.getId()).append(";") .append(DateHelper.getSimpleDateString(date)).append(";"); if (mmiChartDataMap.get(mmiChart.getId()) != null) { chart.append(mmiChartDataMap.get(mmiChart.getId()).toString()); } else { chart.append("0"); } chart.append("\n"); } } return chart.toString(); } private void proccessFileSave() { Calendar date = new GregorianCalendar(); LOGGER.info("getMmiExportDir() returns :" + getMmiExportDir()); LOGGER.info("System.getProperty() returns :" + System.getProperty("CustomerAppsDir")); try { String fileNameTest = System.getProperty("CustomerAppsDir") + File.separator + "cora" + File.separator + "MMI" + File.separator + "export" + File.separator + "chart_" + DateHelper.getSimpleDateString(date) + ".csv"; LOGGER.info("Export data to (test): " + fileNameTest); File myFileTest = new File(fileNameTest); FileUtils.writeStringToFile(myFileTest, getTodaysDataFromDB(), ENCODING_UTF8); } catch (IOException e) { LOGGER.error("Could not write file: " + e.getMessage(), e); } try { String fileNameProd = File.separator + "home1" + System.getProperty("CustomerAppsDir") + File.separator + "cora" + File.separator + "MMI" + File.separator + "export" + File.separator + "chart_" + DateHelper.getSimpleDateString(date) + ".csv"; LOGGER.info("Export data to (prod): " + fileNameProd); File myFileProd = new File(fileNameProd); FileUtils.writeStringToFile(myFileProd, getTodaysDataFromDB(), ENCODING_UTF8); } catch (IOException e) { LOGGER.error("Could not write file: " + e.getMessage(), e); } } class MyClient implements Client { public MyClient() { } public void processEvent(Event event) { switch (event.getType()) { case Event.MARKET_DATA_ITEM_EVENT: processMarketDataItemEvent((MarketDataItemEvent) event); break; case Event.MARKET_DATA_SVC_EVENT: processMarketDataSvcEvent((MarketDataSvcEvent) event); break; case Event.CONNECTION_EVENT: processConnectionEvent((ConnectionEvent) event); break; case Event.COMPLETION_EVENT: processCompletionEvent(event); break; case Event.ENTITLEMENTS_AUTHENTICATION_EVENT: processEntitlementsAuthenticationEvent(event); break; default: LOGGER.error("Unhandled event type: " + event.getType()); break; } } protected void processMarketDataItemEvent(MarketDataItemEvent marketDataItemEvent) { if ((marketDataItemEvent.getMarketDataMsgType() == MarketDataItemEvent.IMAGE) || (marketDataItemEvent.getMarketDataMsgType() == MarketDataItemEvent.UNSOLICITED_IMAGE) || (marketDataItemEvent.getMarketDataMsgType() == MarketDataItemEvent.UPDATE) || (marketDataItemEvent.getMarketDataMsgType() == MarketDataItemEvent.CORRECTION) || (marketDataItemEvent.getMarketDataMsgType() == MarketDataItemEvent.CLOSING_RUN)) { processTibMsg(marketDataItemEvent); } else if (marketDataItemEvent.getMarketDataMsgType() == MarketDataItemEvent.STATUS) { processStatus(marketDataItemEvent); } else if (marketDataItemEvent.getMarketDataMsgType() == MarketDataItemEvent.GROUP_CHANGE) { processGroupChange(marketDataItemEvent); } else if (marketDataItemEvent.getMarketDataMsgType() == MarketDataItemEvent.RENAME) { processRename(marketDataItemEvent); } else { LOGGER.info("Received MARKET_DATA_ITEM_EVENT (misc), service = " + marketDataItemEvent.getServiceName() + ", item = " + marketDataItemEvent.getItemName()); } } protected void processTibMsg(MarketDataItemEvent marketDataItemEvent) { byte[] data = marketDataItemEvent.getData(); int length = ((data != null) ? data.length : 0); LOGGER.info("Received MARKET_DATA_ITEM_EVENT (data), service = " + marketDataItemEvent.getServiceName() + ", dataFormat = " + marketDataItemEvent.getDataFormat() + ", msgType = " + marketDataItemEvent.getMarketDataMsgType() + ", item = " + marketDataItemEvent.getItemName() + ", status = " + marketDataItemEvent.getStatus() + ", datasize = " + length); if (length == 0) { return; } try { TibMsg tibMsg = new TibMsg(); tibMsg.ReUse(); tibMsg.ReUse(); tibMsg.UnPack(data); if (marketDataItemEvent.getStatus().toString().contains("state: OK")) { List mmiChartList = mmiChartService.getByRic(marketDataItemEvent.getItemName()); for (MmiChart mmiChart : mmiChartList) { if (!mmiChart.getStopProcessing()) { try { float fieldVal = getFieldVal(tibMsg, mmiChart); proccessDBRecord(mmiChart.getId(), fieldVal, "comment"); } catch (MessageKeyException te) { exceptionOccured(te.getMessage(getMessages())); } } } } LOGGER.info("Processed data with TibMsg."); } catch (TibException te) { exceptionOccured(getMessages().getMessage("mmi.error.rmmic.dataNotReadable", new Object[] { marketDataItemEvent.getItemName() })); LOGGER.error("Unable to unpack data with TibMsg. Displaying hex.", te); String rawData = HexDump.hexDump(data, length); LOGGER.error(rawData); } } float getFieldVal(TibMsg tibMsg, MmiChart mmiChart) throws MessageKeyException { TibField field = null; float val = 0; int start = mmiChart.getRicStartPosition(); int end = mmiChart.getRicEndPosition(); try { field = tibMsg.Get(mmiChart.getRicRow()); } catch (TibException te) { LOGGER.error("TibException; Chart-ID - RIC - Row: " + mmiChart.getId() + " - " + mmiChart.getRic() + " - " + mmiChart.getRicRow(), te); throw new MessageKeyException("mmi.error.rmmic.wrongRicRow", new Object[] { mmiChart.getRicRow(), mmiChart.getId() }); } try { String feed = field.StringData(); LOGGER.info("parsing feed: " + feed + " from " + start + " to " + end); if (feed == null) { throw new NullPointerException(); } if (end == 0) { end = feed.length(); } if (!feed.substring(start, end).trim().isEmpty()) { val = Float.parseFloat(feed.substring(start, end)); } else { LOGGER.info("Cant parse feed, field value is empty"); } } catch (NullPointerException e) { LOGGER.error("Cant parse feed, field is empty: \'" + field.StringData() + "\' parsed substring " + start + " to " + end, e); throw new MessageKeyException("mmi.error.rmmic.noData", new Object[] { mmiChart.getId() }); } catch (IndexOutOfBoundsException e) { LOGGER.error("Cant parse feed, start- or end-value is wrong: \'" + field.StringData() + "\', parsed substring " + start + " to " + end, e); throw new MessageKeyException("mmi.error.rmmic.wrongStartEnd", new Object[] { start, end, mmiChart.getId() }); } catch (NumberFormatException e) { LOGGER.error("Cant parse feed, field value is not a number: \'" + field.StringData() + "\', parsed substring " + start + " to " + end, e); throw new MessageKeyException("mmi.error.rmmic.dataNan", new Object[] { mmiChart.getId() }); } return val; } protected void proccessDBRecord(int id, float sum, String comment) { LOGGER.info(" ---> new feed: id=" + id + ", date=" + _date + ", sum=" + sum + ", comment=" + comment); DBRecord rec = new DBRecord(); rec.setId(id); rec.setDate(_date); rec.setSum(sum); rec.setComment(comment); _records.add(rec); } protected void processStatus(MarketDataItemEvent marketDataItemEvent) { LOGGER.info("Received MARKET_DATA_ITEM_EVENT (status), service = " + marketDataItemEvent.getServiceName() + ", item = " + marketDataItemEvent.getItemName() + ", status = " + marketDataItemEvent.getStatus().toString()); MarketDataItemStatus marketDataItemStatus = marketDataItemEvent.getStatus(); if (marketDataItemStatus.getState() == MarketDataItemStatus.CLOSED) { exceptionOccured(getMessages().getMessage("mmi.error.rmmic.ricClosed", new Object[] { marketDataItemEvent.getItemName(), marketDataItemStatus.getStatusCode(), marketDataItemStatus.getStatusText() })); } } protected void processGroupChange(MarketDataItemEvent marketDataItemEvent) { LOGGER.info("Received MARKET_DATA_ITEM_EVENT( group_merge ), service = " + marketDataItemEvent.getServiceName() + ", item = " + marketDataItemEvent.getItemName() + ", new group ID = " + marketDataItemEvent.getGroupID()); } protected void processRename(MarketDataItemEvent marketDataItemEvent) { LOGGER.info("Received MARKET_DATA_ITEM_EVENT( rename ), service = " + marketDataItemEvent.getServiceName() + ", item = " + marketDataItemEvent.getItemName() + ", new item name = " + marketDataItemEvent.getNewItemName()); } protected void processMarketDataSvcEvent(MarketDataSvcEvent marketDataSvcEvent) { LOGGER.info("Received MARKET_DATA_SVC_EVENT: " + marketDataSvcEvent + "\n"); } protected void processConnectionEvent(ConnectionEvent connectionEvent) { LOGGER.info("Received CONNECTION_EVENT: " + connectionEvent.getConnectionName() + ", " + connectionEvent.getConnectionStatus().toString()); connectionEvent.getConnectionStatus().getState(); if (connectionEvent.getConnectionStatus().getState() == ConnectionStatus.DOWN && !connectionEvent.getConnectionStatus().getStatusText().equals("uninitialized")) { exceptionOccured(getMessages().getMessage("mmi.error.rmmic.noConnectionEstablished")); } } protected void processCompletionEvent(Event event) { LOGGER.info("Received COMPLETION_EVENT for handle " + event.getHandle()); } protected void processEntitlementsAuthenticationEvent(Event event) { LOGGER.info("Received ENTITLEMENTS_AUTHENTICATION_EVENT: " + ((EntitlementsAuthenticationEvent) event).getStatus()); } } public void executeInternal() { try { init(); run(); cleanup(); proccessDatabaseActions(); proccessFileSave(); } catch (MessageKeyException e) { exceptionOccured(e.getMessage(getMessages())); } catch (Exception e) { LOGGER.error("Unexpected exception occured:", e); exceptionOccured(getMessages().getMessage("mmi.error.chart.intern")); } } public void executeExternal() throws MessagesException { executeInternal(); if (!errorMessages.isEmpty()) { throw new MessagesException(errorMessages); } } @Override protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { LOGGER.info("Execute start"); executeInternal(); if (!errorMessages.isEmpty()) { sendMail(); } LOGGER.info("Execute end"); } private void sendMail() { try { Properties mailConfig = PropertiesLoader.getInstance().getProperties(StringConstants.MAIL_MMI_CHART_TRANSFER); String sender = mailConfig.getProperty("mail.sender"); String recipient = mailConfig.getProperty("mail.recipient"); String subject = mailConfig.getProperty("msg.subject"); String text = mailConfig.getProperty("msg.text"); text = text.replaceAll(ERROR_TAG, ErrorTextHelper.getErrorPlainText(errorMessages)); BasicEmail mail = new BasicEmail(); mail.send(recipient, sender, null, subject, text); } catch (Exception e) { LOGGER.error("Unexpected exception occured:", e); } } }