package nz.ac.lconz.irr.dspace.app.xmlui.aspect.addthisbutton;

import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.environment.SourceResolver;
import org.dspace.app.xmlui.cocoon.AbstractDSpaceTransformer;
import org.dspace.app.xmlui.utils.UIException;
import org.dspace.app.xmlui.wing.WingException;
import org.dspace.app.xmlui.wing.element.Item;
import org.dspace.app.xmlui.wing.element.List;
import org.dspace.app.xmlui.wing.element.Options;
import org.dspace.app.xmlui.wing.element.PageMeta;
import org.dspace.authorize.AuthorizeException;
import org.dspace.core.ConfigurationManager;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

/**
 * @author Andrea Schweer schweer@waikato.ac.nz
 *
 * Cocoon sitemap transformer that adds an AddThis button to the page.
 *
 */
public class AddThisTransformer extends AbstractDSpaceTransformer {
	private String addThisPublisher;

	@Override
	public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters) throws ProcessingException, SAXException, IOException {
		super.setup(resolver, objectModel, src, parameters);
		String publisherIdValue = ConfigurationManager.getProperty("lconz-aspect", "addthis.publisher.id");
		if (publisherIdValue != null && !"".equals(publisherIdValue)) {
			addThisPublisher = publisherIdValue;
			getLogger().info("AddThisTransformer: using AddThis publisher id " + addThisPublisher);
		}
	}

	@Override
	public void addPageMeta(PageMeta pageMeta) throws SAXException, WingException, UIException, SQLException, IOException, AuthorizeException {
		if (addThisPublisher == null) {
			getLogger().debug("AddThisTransformer: no AddThis publisher id set, not adding javascript link");
			return;
		}
		pageMeta.addMetadata("javascript", "addthis").addContent("http://s7.addthis.com/js/250/addthis_widget.js#pubid=" + addThisPublisher);
	}

	@Override
	public void addOptions(Options options) throws SAXException, WingException, UIException, SQLException, IOException, AuthorizeException {
		if (addThisPublisher == null) {
			getLogger().debug("AddThisTransformer: no AddThis publisher id set, not adding button");
			return;
		}
		List shareOption = options.addList("share");
		Item item = shareOption.addItem();
		item.addXref("http://www.addthis.com/bookmark.php?v=250&amp;pubid=" + addThisPublisher, "", "addthis_button");
		item.addFigure("http://s7.addthis.com/static/btn/v2/lg-share-en.gif", "Bookmark and Share", "addthis_button");
	}
}

@@ -2314,16 +2314,48 @@
 hare%22);%0A
+%09%09shareOption.setHead(%22Share%22);%0A
 %09%09Item i
@@ -2397,183 +2397,210 @@
 .add
-Xref(%22http://www.addthis.com/bookmark.php?v=250&amp;pubid=%22 + addThisPublisher, %22%22, %22addthis_button%22);%0A%09%09item.addFigure(%22http://s7.addthis.com/static/btn/v2/lg-share-en.gif%22, 
+Figure(%22http://s7.addthis.com/static/btn/v2/lg-share-en.gif%22,%0A%09%09%09%09              %22http://www.addthis.com/bookmark.php?v=250&amp;pubid=%22 + addThisPublisher,%0A%09%09%09%09              %22addthis_button%22).addContent(
 %22Boo
@@ -2619,26 +2619,8 @@
 are%22
-, %22addthis_button%22
 );%0A%09

Fix creation of button HTML