package uwl.rss.portal;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.sap.netweaver.bc.uwl.Item;
import com.sap.netweaver.bc.uwl.ProviderStatus;
import com.sap.netweaver.bc.uwl.StatusEnum;
import com.sap.netweaver.bc.uwl.UWLContext;
import com.sap.netweaver.bc.uwl.UWLException;
import com.sap.netweaver.bc.uwl.config.Action;
import com.sap.netweaver.bc.uwl.connect.ConnectorException;
import com.sap.netweaver.bc.uwl.connect.IActionHandler;
import com.sap.netweaver.bc.uwl.connect.IProviderConnector;
import com.sap.netweaver.bc.uwl.connect.IPushChannel;
import com.sap.tc.logging.Category;
import com.sap.tc.logging.Location;
import com.sap.tc.logging.Severity;

/**
 * @author Sumit Gogia,SAP
 *
 */
public class ActionHandler implements IActionHandler {

	private static final Location loc = Location.getLocation(ActionHandler.class);
	private IPushChannel pushChannel;
	private IProviderConnector connector;

	public ActionHandler(IPushChannel pc, IProviderConnector connector) {
		this.pushChannel = pc;
		this.connector = connector;
	}

	/* (non-Javadoc)
	 * @see com.sap.netweaver.bc.uwl.connect.IActionHandler#getId()
	 */
	public String getId() {
		return Action.PROVIDER_ACTION_HANDLER;
	}

	/* (non-Javadoc)
	 * @see com.sap.netweaver.bc.uwl.connect.IActionHandler#getUrl(com.sap.netweaver.bc.uwl.UWLContext, com.sap.netweaver.bc.uwl.Item, com.sap.netweaver.bc.uwl.config.Action, java.util.Map)
	 */
	public String getUrl(UWLContext arg0, Item arg1, Action arg2, Map arg3)
		throws UWLException {
		return null;
	}

	/* (non-Javadoc)
	 * @see com.sap.netweaver.bc.uwl.connect.IActionHandler#isLauncher()
	 */
	public boolean isLauncher() {
		return false;
	}

	/* (non-Javadoc)
	 * @see com.sap.netweaver.bc.uwl.connect.IActionHandler#needsItemValidation()
	 */
	public boolean needsItemValidation() {
		return false;
	}

	/* (non-Javadoc)
	 * @see com.sap.netweaver.bc.uwl.connect.IActionHandler#performAction(com.sap.netweaver.bc.uwl.UWLContext, com.sap.netweaver.bc.uwl.Item, com.sap.netweaver.bc.uwl.config.Action, java.util.Map)
	 */
	public ProviderStatus performAction(UWLContext context,
										Item item,
										Action action,
										java.util.Map properties)
										throws ConnectorException {
		try {
			String actionName = action.getName();
			if(Action.COMPLETE.equals(actionName)) {
				//complete the item
				item.setStatus(StatusEnum.COMPLETED);
				item.setCompletedDate(new Date());
				item.setItemType(UwlRssConnector.RSS_COMPLETED_ITEM_TYPE);
				pushChannel.updateItem(connector,context,item);
			}
			else if(Action.DELETE.equals(actionName)) {
				/*
				 * If we delete the item, then it might show up again, if fetched from
				 * the rss source.
				 * So we first change the type of the item to uwl.deleted.rss
				 * and we also set the expiry time to 30 days, so that the uwl cache
				 * does not grow in an uncontrolled way.
				 * 
				 * we could make the expiry time configurable also.
				 */
				item.setItemType(UwlRssConnector.RSS_DELETED_ITEM);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(Calendar.DAY_OF_YEAR, 30);
				Date expDate = new Date(calendar.getTimeInMillis());
				item.setExpiryDate(expDate);
				pushChannel.updateItem(connector,context,item);
			}
			else if(Action.MARK_AS_READ.equals(actionName)) {
				item.setStatus(StatusEnum.READ);
				pushChannel.updateItem(connector,context,item);
			}
		}
		catch(UWLException ue) {
			Category.APPLICATIONS.logThrowable(Severity.FATAL,
												loc,
												(Object)"performAction()",
												new Object[]{this},
												"Error updating item through pushChannel",
												ue);
			throw new ConnectorException(ue);
		}
		
		ProviderStatus  status = new ProviderStatus(true,null, Action.PROVIDER_ACTION_HANDLER);;
		return status;
	}

}
