Package de.nava.informa.core

Examples of de.nava.informa.core.CategoryIF


    SessionHandler handler = SessionHandler.getInstance();
    Session session = handler.getSession();
    ChannelBuilder builder = new ChannelBuilder(session);
   
    // --- create a new cat
    CategoryIF catA = builder.createCategory(null, "News Category");
    Long catId = null;
   
    Transaction tx = null;
    try {
      tx = session.beginTransaction();
      session.save(catA);
      tx.commit();
      catId = new Long(catA.getId());
      System.out.println("Saved category with id " + catId + " persistently");
    }
    catch (HibernateException he) {
      if (tx != null) tx.rollback();
      throw he;
View Full Code Here


    ChannelBuilder builder = new ChannelBuilder(session);
    Transaction tx = null;
    int chId = -1;
    // our test objects
    ChannelIF channel;
    CategoryIF cat1, cat2;
    // -- first create a channel with a category assigned
    try {
      tx = session.beginTransaction();
      // create channel
      String chanName = "Foo Test Channel";
      channel = builder.createChannel(chanName);
      channel.setDescription("Test Channel: " + chanName);
      session.saveOrUpdate(channel);
      // create cat1
      cat1 = builder.createCategory(null, "Root Cat");
      session.saveOrUpdate(cat1);
      // create cat2
      cat2 = builder.createCategory(cat1, "Agent_A");
      session.saveOrUpdate(cat2);
      channel.addCategory(cat2);
      session.saveOrUpdate(channel);
      tx.commit();
      chId = (int) channel.getId();
    }
    catch (HibernateException he) {
      logger.warn("trying to rollback the transaction");
      if (tx != null) tx.rollback();
      throw he;
    }
    assertTrue("No valid channel created.", chId >= 0);
    // -- try to retrieve channel and the assigned category
    try {
      logger.info("Searching for channel " + chId);
      Object result = session.get(Channel.class, new Long(chId));
      assertNotNull(result);
      ChannelIF c = (ChannelIF) result;
      logger.info("retrieved channel --> " + c);
      assertEquals(1, c.getCategories().size());
      CategoryIF cat = (CategoryIF) c.getCategories().iterator().next();
      assertEquals("Agent_A", cat.getTitle());
      assertNotNull(cat.getParent());
      assertEquals("Root Cat", cat.getParent().getTitle());
    }
    catch (HibernateException he) {
      logger.warn("Error while querying for channel");
      throw he;
    }
View Full Code Here

    // create channel builder to help create CategoryIF objects
    ChannelBuilder builder = new ChannelBuilder();

    // create current CategoryIF object; Parent may be null if at top level
    CategoryIF cat = builder.createCategory(parent, title);
    // iterate off list of keys from children list
    Enumeration itChild = children.keys();
    while (itChild.hasMoreElements()) {
      String childKey = (String) itChild.nextElement();
      // don't need to keep track of return CategoryIF since it will be added as child of another instance
View Full Code Here

        // transform cat list & hierarchy into list of CategoryIF elements
        Enumeration<String> enumCategories = catTable.keys();
        while (enumCategories.hasMoreElements()) {
          String key = enumCategories.nextElement();
          // build category list: getCategoryList(parent, title, children)
          CategoryIF cat = getCategoryList(null, key, catTable.get(key));
          catList.add(cat);
        }
        if (catList.size() > 0) {
          // if categories were actually created, then add list to item node
          rssItem.setCategories(catList);
        }
      }

      // get publication date
      Element elDate = item.getChild("pubDate", defNS);
      if (elDate == null) {
        // fallback mechanism: get dc:date element
        elDate = item.getChild("date", dcNS);
      }
      if (elDate != null) {
        rssItem.setDate(ParserUtils.getDate(elDate.getTextTrim()));
      }

      rssItem.setFound(dateParsed);

      // get Author element
      Element elAuthor = item.getChild("author", defNS);
      if (elAuthor == null) {
        // fallback mechanism: get dc:creator element
        elAuthor = item.getChild("creator", dcNS);
      }
      if (elAuthor != null)
        rssItem.setCreator(elAuthor.getTextTrim());

      // get Comments element
      Element elComments = item.getChild("comments", defNS);
      String strComments = "";
      if (elComments != null) {
        strComments = elComments.getTextTrim();
      }
      rssItem.setComments(ParserUtils.getURL(strComments));

      // get guid element
      Element elGuid = item.getChild("guid", defNS);
      if (elGuid != null) {
        String guidUrl = elGuid.getTextTrim();
        if (guidUrl != null) {
          boolean permaLink = true;
          Attribute permaLinkAttribute = elGuid.getAttribute("isPermaLink",
              defNS);
          if (permaLinkAttribute != null) {
            String permaLinkStr = permaLinkAttribute.getValue();
            if (permaLinkStr != null) {
              permaLink = Boolean.valueOf(permaLinkStr).booleanValue();
            }
          }
          ItemGuidIF itemGuid = cBuilder.createItemGuid(rssItem, guidUrl,
              permaLink);
          rssItem.setGuid(itemGuid);
        }
      }

      // get source element
      Element elSource = item.getChild("source", defNS);
      if (elSource != null) {
        String sourceName = elSource.getTextTrim();
        Attribute sourceAttribute = elSource.getAttribute("url", defNS);
        if (sourceAttribute != null) {
          String sourceLocation = sourceAttribute.getValue().trim();
          ItemSourceIF itemSource = cBuilder.createItemSource(rssItem,
              sourceName, sourceLocation, null);
          rssItem.setSource(itemSource);
        }
      }

      // get enclosure element
      Element elEnclosure = item.getChild("enclosure", defNS);
      if (elEnclosure != null) {
        URL location = null;
        String type = null;
        int length = -1;
        Attribute urlAttribute = elEnclosure.getAttribute("url", defNS);
        if (urlAttribute != null) {
          location = ParserUtils.getURL(urlAttribute.getValue().trim());
        }
        Attribute typeAttribute = elEnclosure.getAttribute("type", defNS);
        if (typeAttribute != null) {
          type = typeAttribute.getValue().trim();
        }
        Attribute lengthAttribute = elEnclosure.getAttribute("length", defNS);
        if (lengthAttribute != null) {
          try {
            length = Integer.parseInt(lengthAttribute.getValue().trim());
          } catch (NumberFormatException e) {
            logger.warn(e);
          }
        }
        ItemEnclosureIF itemEnclosure = cBuilder.createItemEnclosure(rssItem,
            location, type, length);
        rssItem.setEnclosure(itemEnclosure);
      }
    }

    // 0..1 image element
    Element image = channel.getChild("image", defNS);
    if (image != null) {

      ParserUtils.matchCaseOfChildren(image, new String[] { "title", "url",
          "link", "width", "height", "description" });

      ImageIF rssImage = cBuilder.createImage(image.getChildTextTrim("title",
          defNS), ParserUtils.getURL(image.getChildTextTrim("url", defNS)),
          ParserUtils.getURL(image.getChildTextTrim("link", defNS)));
      Element imgWidth = image.getChild("width", defNS);
      if (imgWidth != null) {
        try {
          rssImage.setWidth(Integer.parseInt(imgWidth.getTextTrim()));
        } catch (NumberFormatException e) {
          logger.warn("Error parsing width: " + e.getMessage());
        }
      }
      Element imgHeight = image.getChild("height", defNS);
      if (imgHeight != null) {
        try {
          rssImage.setHeight(Integer.parseInt(imgHeight.getTextTrim()));
        } catch (NumberFormatException e) {
          logger.warn("Error parsing height: " + e.getMessage());
        }
      }
      Element imgDescr = image.getChild("description", defNS);
      if (imgDescr != null) {
        rssImage.setDescription(imgDescr.getTextTrim());
      }
      chnl.setImage(rssImage);
    }

    // 0..1 textinput element
    Element txtinp = channel.getChild("textinput", defNS);
    if (txtinp != null) {

      ParserUtils.matchCaseOfChildren(txtinp, new String[] { "title",
          "description", "name", "link" });

      TextInputIF rssTextInput = cBuilder.createTextInput(txtinp
          .getChildTextTrim("title", defNS), txtinp.getChildTextTrim(
          "description", defNS), txtinp.getChildTextTrim("name", defNS),
          ParserUtils.getURL(txtinp.getChildTextTrim("link", defNS)));
      chnl.setTextInput(rssTextInput);
    }

    // 0..1 copyright element
    Element copyright = channel.getChild("copyright", defNS);
    if (copyright != null) {
      chnl.setCopyright(copyright.getTextTrim());
    }

    // 0..1 Rating element
    Element rating = channel.getChild("rating", defNS);
    if (rating != null) {
      chnl.setRating(rating.getTextTrim());
    }

    // 0..1 Docs element
    Element docs = channel.getChild("docs", defNS);
    if (docs != null) {
      chnl.setDocs(docs.getTextTrim());
    }

    // 0..1 Generator element
    Element generator = channel.getChild("generator", defNS);
    if (generator != null) {
      chnl.setGenerator(generator.getTextTrim());
    }

    // 0..1 ttl element
    Element ttl = channel.getChild("ttl", defNS);
    if (ttl != null) {
      String ttlValue = ttl.getTextTrim();
      try {
        chnl.setTtl(Integer.parseInt(ttlValue));
      } catch (NumberFormatException e) {
        logger.warn("Invalid TTL format: '" + ttlValue + "'");
      }
    }

    // 0..1 pubDate element
    Element pubDate = channel.getChild("pubDate", defNS);
    if (pubDate != null) {
      chnl.setPubDate(ParserUtils.getDate(pubDate.getTextTrim()));
    }

    // 0..1 lastBuildDate element
    Element lastBuildDate = channel.getChild("lastBuildDate", defNS);
    if (lastBuildDate != null) {
      chnl.setLastBuildDate(ParserUtils.getDate(lastBuildDate.getTextTrim()));
    }

    // get category list
    // get list of <category> elements
    List listCategory = channel.getChildren("category", defNS);
    if (listCategory.size() < 1) {
      // fallback mechanism: get dc:category element
      listCategory = channel.getChildren("category", dcNS);
    }
    if (listCategory.size() > 0) {
      RecursiveHashtable<String> catTable = new RecursiveHashtable<String>();
      // for each category, parse hierarchy
      Iterator itCat = listCategory.iterator();
      while (itCat.hasNext()) {
        RecursiveHashtable<String> currTable = catTable;
        Element elCategory = (Element) itCat.next();
        // get contents of category element
        String[] titles = elCategory.getTextNormalize().split("/");
        for (int x = 0; x < titles.length; x++) {
          // tokenize category string to extract out hierarchy
          if (currTable.containsKey(titles[x]) == false) {
            // if token does not exist in current map, add it with child Hashtable
            currTable.put(titles[x], new RecursiveHashtable<String>());
          }
          // reset current Hashtable to child's Hashtable then iterate to next token
          currTable = currTable.get(titles[x]);
        }
      }
      ArrayList<CategoryIF> catList = new ArrayList<CategoryIF>();
      // transform cat list & hierarchy into list of CategoryIF elements
      Enumeration<String> enumCategories = catTable.keys();
      while (enumCategories.hasMoreElements()) {
        String key = enumCategories.nextElement();
        // build category list: getCategoryList(parent, title, children)
        CategoryIF cat = getCategoryList(null, key, catTable.get(key));
        catList.add(cat);
      }
      if (catList.size() > 0) {
        // if categories were actually created, then add list to item node
        chnl.setCategories(catList);
View Full Code Here

  public ItemGuidIF createItemGuid(ItemIF item, String location, boolean permaLink) {
    return new ItemGuid(item, location, permaLink);
  }

  public CategoryIF createCategory(CategoryIF parent, String title) {
    CategoryIF cat = new Category(title);
    if (parent != null) {
      parent.addChild(cat);
    }
    return cat;
  }
View Full Code Here

  public boolean equals(Object obj) {
    if (!(obj instanceof CategoryIF)) {
      return false;
    }
    CategoryIF cmp = (CategoryIF) obj;

    return cmp.getTitle().equals(title)
      && (cmp.getId() == id);
  }
View Full Code Here

 
  public CategoryIF createCategory(CategoryIF parent, String title, String domain) {
    Query query = session.createQuery("from Category as cat where cat.title = ? and cat.domain = ? ");
    query.setString(0, title);
    query.setString(1, domain);
    CategoryIF cat = (CategoryIF) query.uniqueResult();
    if (cat == null) {
      cat = new Category(title);
      cat.setDomain(domain);
      if (parent != null) {
        parent.addChild(cat);
      }
      session.save(cat);
    }
View Full Code Here

    */
   public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof CategoryIF)) return false;

      final CategoryIF c = (CategoryIF) o;

      if (domain != null ? !domain.equals(c.getDomain()) : c.getDomain() != null) return false;
      if (title != null ? !title.equals(c.getTitle()) : c.getTitle() != null) return false;

      return true;
   }
View Full Code Here

        if (categories.size() == 0) {
            elem.addContent(new Element("category").setText(l_catString.toString()));
        } else {
            Iterator catIt = categories.iterator();
            while (catIt.hasNext()) {
                CategoryIF childCat = (CategoryIF) catIt.next();
                elem = getCategoryElements(elem, childCat, l_catString);
            }
        }
        return elem;
    }
View Full Code Here

        }
        if (channel.getCategories() != null) {
            Collection categories = channel.getCategories();
            Iterator catIt = categories.iterator();
            while (catIt.hasNext()) {
                CategoryIF cat = (CategoryIF) catIt.next();
                channelElem = getCategoryElements(channelElem, cat, null);
            }
        }

        if (channel.getUpdateBase() != null) {
            channelElem.addContent(new Element("updateBase", syNs).setText(df
                    .format(channel.getUpdateBase())));
        }
        if (channel.getUpdatePeriod() != null) {
            // don't put out frequency without specifying period
            channelElem.addContent(new Element("updateFrequency", syNs)
                    .setText((new Integer(channel.getUpdateFrequency())).toString()));
            channelElem.addContent(new Element("updatePeriod", syNs).setText(channel
                    .getUpdatePeriod().toString()));
        }
        // export channel image           
        if (channel.getImage() != null) {
          Element imgElem = new Element("image");
          imgElem.addContent(new Element("title")
                             .setText(channel.getImage().getTitle()));
          imgElem.addContent(new Element("url")
                             .setText(channel.getImage().getLocation().toString()));
          imgElem.addContent(new Element("link")
                             .setText(channel.getImage().getLink().toString()));
          imgElem.addContent(new Element("height")
                             .setText("" + channel.getImage().getHeight()));
          imgElem.addContent(new Element("width")
                             .setText("" + channel.getImage().getWidth()));
          imgElem.addContent(new Element("description")
                             .setText(channel.getImage().getDescription()));
          channelElem.addContent(imgElem);
        }
        
        // TODO: add exporting textinput field
        //     if (channel.getTextInput() != null) {
        //       channelElem.addContent(channel.getTextInput().getElement());
        //     }
       
        Collection items = channel.getItems();
        Iterator it = items.iterator();
        while (it.hasNext()) {
            ItemIF item = (ItemIF) it.next();
            Element itemElem = new Element("item");
            itemElem.addContent(new Element("title").setText(item.getTitle()));
            if (item.getLink() != null) {
                itemElem.addContent(new Element("link").setText(item.getLink()
                        .toString()));
            }
            if (item.getDescription() != null) {
                itemElem.addContent(new Element("description").setText(item
                        .getDescription()));
            }
            if (item.getCategories() != null) {
                Collection categories = item.getCategories();
                Iterator catIt = categories.iterator();
                while (catIt.hasNext()) {
                    CategoryIF cat = (CategoryIF) catIt.next();
                    itemElem = getCategoryElements(itemElem, cat, null);
                }
            }
            if (item.getDate() != null) {
                itemElem.addContent(new Element("pubDate").setText(ParserUtils
View Full Code Here

TOP

Related Classes of de.nava.informa.core.CategoryIF

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.