Package com.agiletec.plugins.jprssaggregator.apsadmin.aggregator

Source Code of com.agiletec.plugins.jprssaggregator.apsadmin.aggregator.AggregatorAction

/*
*
* Copyright 2013 Entando S.r.l. (http://www.entando.com) All rights reserved.
*
* This file is part of Entando software.
* Entando is a free software;
* You can redistribute it and/or modify it
* under the terms of the GNU General Public License (GPL) as published by the Free Software Foundation; version 2.
*
* See the file License for the specific language governing permissions  
* and limitations under the License
*
*
*
* Copyright 2013 Entando S.r.l. (http://www.entando.com) All rights reserved.
*
*/
package com.agiletec.plugins.jprssaggregator.apsadmin.aggregator;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.agiletec.aps.system.ApsSystemUtils;
import com.agiletec.aps.system.exception.ApsSystemException;
import com.agiletec.aps.system.services.category.Category;
import com.agiletec.aps.system.services.category.ICategoryManager;
import com.agiletec.aps.util.ApsProperties;
import com.agiletec.apsadmin.system.AbstractTreeAction;
import com.agiletec.apsadmin.system.ApsAdminSystemConstants;
import com.agiletec.plugins.jacms.aps.system.services.content.IContentManager;
import com.agiletec.plugins.jacms.aps.system.services.content.model.SmallContentType;
import com.agiletec.plugins.jprssaggregator.aps.system.services.aggregator.ApsAggregatorItem;
import com.agiletec.plugins.jprssaggregator.aps.system.services.aggregator.IAggregatorManager;
import com.agiletec.plugins.jprssaggregator.aps.system.services.converter.IRssConverterManager;
import com.sun.syndication.feed.synd.SyndEntryImpl;

/**
* This class executes the operations for the backend administration
* for the {@link ApsAggregatorItem} objects
*/
public class AggregatorAction extends AbstractTreeAction implements IAggregatorAction {

  @Override
  public void validate() {
    super.validate();
    this.checkLink();
  }
 
  private void checkLink() {
    try {
      if (isValidUrl(this.getLink())) {
        List<SyndEntryImpl> entries = this.getRssConverterManager().getRssEntries("rss_2.0", this.getLink());
        if (null == entries || entries.size() == 0) {
          this.addFieldError("link", this.getText("jprssaggregator.error.link.nofeeds"));
        }
        this.checkDuplicateLink();
      } else {
        this.addFieldError("link", this.getText("jprssaggregator.error.link.invalid"));
      }
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "error in checkLink");
    }
  }

  private void checkDuplicateLink() {
    try {
      List<ApsAggregatorItem> items = this.getAggregatorItems();
      if (null != items && items.size() > 0) {
        for (int i = 0; i < items.size(); i++) {
          ApsAggregatorItem aggregatorItem = items.get(i);
          boolean sameItem = this.getCode() == aggregatorItem.getCode();
          if (this.getLink().equals(aggregatorItem.getLink()) && !sameItem) {
            String arg[] = new String[]{aggregatorItem.getDescr()};
            this.addFieldError("link", this.getText("jprssaggregator.error.link.duplicated", arg));
          }
        }
      }
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "error in checkDuplicateLink");
    }
  }

  private static boolean isValidUrl(String link) {
    boolean valid = false;
    try {
      URL url = new URL(link);
      URLConnection connection = url.openConnection();
      connection.setRequestProperty("User-Agent", "jAPS jprssaggregaror");
      if (null != connection.getContent()) return true;
    } catch (Throwable t) {
      ApsSystemUtils.getLogger().info("unable to parse url");
    }
    return valid;
  }
 
  @Override
  public List<ApsAggregatorItem> getAggregatorItems() {
    List<ApsAggregatorItem> items = null;
    try {
      items = this.getAggregatorManager().getItems();
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "getAggregatorItems");
    }
    return items;
  }
 
  @Override
  public String newItem() {
    try {
      this.setStrutsAction(ApsAdminSystemConstants.ADD);
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "newItem");
      return FAILURE;
    }
    return SUCCESS;
  }

  @Override
  public String save() {
    try {
      int strutsAction = this.getStrutsAction();
      ApsAggregatorItem item = this.createItem();
      if (strutsAction == ApsAdminSystemConstants.ADD) {
        this.getAggregatorManager().addItem(item);
      } else if (strutsAction == ApsAdminSystemConstants.EDIT) {
        this.getAggregatorManager().update(item);
      }
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "save");
      return FAILURE;
    }
    return SUCCESS;
  }

  @Override
  public String edit() {
    try {
      this.setStrutsAction(ApsAdminSystemConstants.EDIT);
      String check = this.populateForm();
      if (null != check) {
        return check;
      }
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "edit");
      return FAILURE;
    }
    return SUCCESS;
  }

  @Override
  public String delete() {
    try {
      ApsAggregatorItem item = this.getAggregatorManager().getItem(this.getCode());
      if (null == item) {
        this.addActionError(this.getText("jprssaggregator.error.itemNull"));
        return INPUT;
      }
      this.getAggregatorManager().deleteItem(item.getCode());
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "deleteItem");
      return FAILURE;
    }
    return SUCCESS;
  }

  @Override
  public List<SmallContentType> getContentTypes() {
    List<String> tempContentTypes = new ArrayList<String>();
    Set<String> mappings = this.getRssConverterManager().getMappings();
    tempContentTypes.addAll(mappings);
    List<SmallContentType> contentTypes = new ArrayList<SmallContentType>();
    for (int i = 0; i < tempContentTypes.size(); i++) {
      String code = tempContentTypes.get(i);
      SmallContentType smallContentType = this.getSmallContentType(code);
      contentTypes.add(smallContentType);
    }
   
    return contentTypes;
  }
 
  @Override
  public String syncronize() {
    try {
      ApsAggregatorItem item = this.getAggregatorManager().getItem(this.getCode());
      this.getAggregatorManager().updateSource(item);
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "syncronize");
      return FAILURE;
    }
    return SUCCESS;
  }

  private ApsAggregatorItem createItem() throws Throwable {
    ApsAggregatorItem item = new ApsAggregatorItem();
    if (getStrutsAction() == ApsAdminSystemConstants.EDIT) {
      item.setCode(this.getCode());
      item.setLastUpdate(new Date(this.getLastUpdate().getTime()));
    }
    System.out.println(this.getContentType());
    item.setContentType(this.getContentType());
    item.setDelay(this.getDelay());
    item.setLink(this.getLink());
    item.setDescr(this.getDescr());
    item.setCategories(this.getCategories());
    return item;
  }

  protected String populateForm() throws ApsSystemException, IOException {
    int code = this.getCode();
    ApsAggregatorItem item = this.getAggregatorManager().getItem(code);
    if (null == item) {
      this.addActionError(this.getText("jprssaggregator.error.itemNotFound"));
      return "itemList";
    }
    if (null != item.getCategories()) {
      this.setXmlCategories(item.getCategories().toXml());
    }
    this.setCode(item.getCode());
    this.setContentType(item.getContentType());
    this.setDescr(item.getDescr());
    this.setDelay(item.getDelay());
    this.setLastUpdate(new Timestamp(item.getLastUpdate().getTime()));
    this.setLink(item.getLink());
    return null;
  }
 
  public Map<Integer, String> getDelays() {
    Map delays = new TreeMap<Integer, String>();
    Iterator it = this._delays.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry pairs = (Map.Entry)it.next();
      String value = (String)pairs.getValue();
      String[] splitted = value.split("_");
      delays.put(pairs.getKey(), splitted[0] + " " + this.getText(splitted[1]));
    }
    return delays;
  }
 
  public Category getCategoryRoot() {
    return this.getCategoryManager().getRoot();
  }
 
  public Category getCategory(String categoryCode) {
    return this.getCategoryManager().getCategory(categoryCode);
  }
 
  public String joinCategory() {
    try {
      String categoryCode = this.getCategoryCode();
      Category category = this.getCategoryManager().getCategory(categoryCode);
      if (null != category && !category.getCode().equals(category.getParentCode())) {
        ApsProperties currentCats = this.getCategories();
        currentCats.put(category.getCode(), category.getFullTitle());
        this.setXmlCategories(currentCats.toXml());
      }
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "joinCategory");
      return FAILURE;
    }
    return SUCCESS;
  }

  public String removeCategory() {
    try {
      String categoryCode = this.getCategoryCode();
      Category category = this.getCategoryManager().getCategory(categoryCode);
      if (null != category) {
        ApsProperties currentCats = this.getCategories();
        currentCats.remove(categoryCode);
        this.setXmlCategories(currentCats.toXml());
      }
    } catch (Throwable t) {
      ApsSystemUtils.logThrowable(t, this, "removeCategory");
      return FAILURE;
    }
    return SUCCESS;
  }

  public ApsProperties getCategories() throws Throwable {
    ApsProperties properties = new ApsProperties();
    if (null != this.getXmlCategories() && this.getXmlCategories().trim().length() > 0) {
      properties.loadFromXml(this.getXmlCategories());
    }
    this.purgeCategories(properties);
    return properties;
  }
 
  /**
   * Rimuove eventuali categorie non più esistenti
   * @param properties
   */
  private void purgeCategories(ApsProperties properties) {
    for (Enumeration e = properties.keys() ; e.hasMoreElements() ;) {
      String categoryCode = (String) e.nextElement();
      Category category = this.getCategoryManager().getCategory(categoryCode);
      if (null == category) {
        properties.remove(categoryCode);
      }
    }
  }

  public SmallContentType getSmallContentType(String code) {
    SmallContentType smallContentType = null;
    Map<String, SmallContentType> smallContentTypes = this.getContentManager().getSmallContentTypesMap();
    if (smallContentTypes.containsKey(code)) {
      smallContentType = (SmallContentType) smallContentTypes.get(code);
    }
    return smallContentType;
  }
 
  public void setDelays(Map<Integer, String> delays) {
    this._delays = delays;
  }

  public void setCategoryCode(String categoryCode) {
    this._categoryCode = categoryCode;
  }
  public String getCategoryCode() {
    return _categoryCode;
  }

  public void setXmlCategories(String xmlCategories) {
    this._xmlCategories = xmlCategories;
  }
  public String getXmlCategories() {
    return _xmlCategories;
  }

  public int getCode() {
    return _code;
  }
  public void setCode(int code) {
    this._code = code;
  }

  public void setContentType(String contentType) {
    this._contentType = contentType;
  }
  public String getContentType() {
    return _contentType;
  }

  public String getDescr() {
    return _descr;
  }
  public void setDescr(String descr) {
    this._descr = descr;
  }

  public long getDelay() {
    return _delay;
  }
  public void setDelay(long delay) {
    this._delay = delay;
  }

  public String getLink() {
    return _link;
  }
  public void setLink(String link) {
    this._link = link;
  }

  public Timestamp getLastUpdate() {
    return _lastUpdate;
  }
  public void setLastUpdate(Timestamp lastUpdate) {
    this._lastUpdate = lastUpdate;
  }

  public void setStrutsAction(int strutsAction) {
    this._strutsAction = strutsAction;
  }
  public int getStrutsAction() {
    return _strutsAction;
  }
 
  public void setCategoryManager(ICategoryManager categoryManager) {
    this._categoryManager = categoryManager;
  }
  protected ICategoryManager getCategoryManager() {
    return _categoryManager;
  }
 
  public void setAggregatorManager(IAggregatorManager aggregatorManager) {
    this._aggregatorManager = aggregatorManager;
  }
  protected IAggregatorManager getAggregatorManager() {
    return _aggregatorManager;
  }
 
  public void setRssConverterManager(IRssConverterManager rssConverterManager) {
    this._rssConverterManager = rssConverterManager;
  }
  protected IRssConverterManager getRssConverterManager() {
    return _rssConverterManager;
  }
 
  public void setContentManager(IContentManager contentManager) {
    this._contentManager = contentManager;
  }
  protected IContentManager getContentManager() {
    return _contentManager;
  }

  private int _code;
  private String _contentType;
  private String _descr;
  private long _delay;
  private String _link;
  private Timestamp _lastUpdate;
  private String _categoryCode;
  private String _xmlCategories;

  private int _strutsAction;
  private Map<Integer, String> _delays;

  private IAggregatorManager _aggregatorManager;
  private IRssConverterManager _rssConverterManager;
  private ICategoryManager _categoryManager;
  private IContentManager _contentManager;
 
}
TOP

Related Classes of com.agiletec.plugins.jprssaggregator.apsadmin.aggregator.AggregatorAction

TOP
Copyright © 2018 www.massapi.com. 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.