private boolean isCriteriaMatching(NotificationSubscriber notifSubscriber, ProductItem item) { List<Configuration> configurations = notifSubscriber.getCriteria(); for (int i = 0; i < configurations.size(); i++) { Configuration config = configurations.get(i); String condition = config.getCondition(); String attributeName = config.getAttributeName(); String atrributeValue = config.getAttributeValue(); if (attributeName.equalsIgnoreCase("listPrice")) { String itemListPrice = item.getList_price(); if (itemListPrice == null) return false; if (condition.equals("=")) { return (Integer.parseInt(itemListPrice) == Integer.parseInt(atrributeValue)); } else if (condition.equals(">=")) { return (Integer.parseInt(itemListPrice) >= Integer.parseInt(atrributeValue)); } else if (condition.equals("<=")) { return (Integer.parseInt(itemListPrice) <= Integer.parseInt(atrributeValue)); } } else if (attributeName.equalsIgnoreCase("releaseDate")) { String itemReleaseDate = item.getRelease_date(); if (itemReleaseDate == null) return false; DateFormat formatter = new SimpleDateFormat("dd-MM-yy"); } else if (attributeName.equalsIgnoreCase("title")) { String itemTitle = item.getTitle(); if (itemTitle == null) return false; if (condition.equals("=") && !itemTitle.equals(atrributeValue)) { return false; } else if (condition.equals("!=") && itemTitle.equals(atrributeValue)) { return false; } } else if (attributeName.equalsIgnoreCase("authors")) { String itemAuthor = item.getAuthor(); if (itemAuthor == null) return false; if (condition.equals("=") && !itemAuthor.equals(atrributeValue)) { return false; } else if (condition.equals("!=") && itemAuthor.equals(atrributeValue)) { return false; } } else if (attributeName.equalsIgnoreCase("publisher")) { String itemPublisher = item.getPublisher(); if (itemPublisher == null) return false; if (condition.equals("=") && !itemPublisher.equals(atrributeValue)) { return false; } else if (condition.equals("!=") && itemPublisher.equals(atrributeValue)) { return false; } } } return true; }
private void BuildSubscribersFromXML() { // Reading XML File DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = builderFactory.newDocumentBuilder(); Document dom = db.parse(new File(ConfigurationFile)); Element root = dom.getDocumentElement(); NodeList subscribersList = root.getElementsByTagName("Subscriber"); for (int i = 0, listLength = subscribersList.getLength(); i < listLength; i++) { Element subscriberElement = (Element) subscribersList.item(i); String subscriberName = subscriberElement.getAttribute("name"); String implClassName = subscriberElement.getAttribute("implclass"); List<Configuration> criteria = new ArrayList<Configuration>(); NodeList configsList = subscriberElement.getElementsByTagName("Configurations"); if (configsList != null && configsList.getLength() > 0) { Element configsRoot = (Element) configsList.item(0); NodeList configurations = configsRoot.getElementsByTagName("config"); for (int j = 0; j < configurations.getLength(); j++) { Element configElement = (Element) configurations.item(j); String attributeName = configElement.getAttribute("attributename"); String attributeValue = configElement.getAttribute("attributevalue"); String condition = configElement.getAttribute("condition"); Configuration config = new Configuration(); config.setAttributeName(attributeName); config.setAttributeValue(attributeValue); config.setCondition(condition); criteria.add(config); } } NotificationSubscriber notifSubscriber = new NotificationSubscriber(subscriberName, implClassName, criteria); addSubscriber(notifSubscriber); } System.out.println("Notification Subscriber data built successfully"); } catch (Exception e) { e.printStackTrace(); } }