Project

General

Profile

1
package eu.dnetlib.actionmanager.actions;
2

    
3
import java.util.List;
4
import java.util.Map;
5

    
6
import javax.xml.transform.Transformer;
7
import javax.xml.transform.TransformerFactory;
8

    
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.dom4j.Document;
12
import org.dom4j.io.DocumentSource;
13
import org.dom4j.io.SAXReader;
14
import org.springframework.beans.factory.annotation.Required;
15
import org.springframework.core.io.Resource;
16

    
17
import com.google.common.collect.Lists;
18

    
19
import eu.dnetlib.actionmanager.common.Agent;
20
import eu.dnetlib.actionmanager.common.Operation;
21
import eu.dnetlib.actionmanager.common.Provenance;
22
import eu.dnetlib.actionmanager.rmi.ActionManagerException;
23
import eu.dnetlib.data.proto.TypeProtos.Type;
24
import eu.dnetlib.miscutils.datetime.DateUtils;
25

    
26
/**
27
 * Factory bean to create Actions bypassing the Action Manager API
28
 * 
29
 * @author michele
30
 */
31
public class ActionFactory {
32

    
33
	private static final Log log = LogFactory.getLog(ActionFactory.class);
34

    
35
	private Map<String, Resource> xslts;
36

    
37
	final TransformerFactory tFactory = TransformerFactory.newInstance();
38

    
39
	/**
40
	 * Creates a list of AtomicAction(s) to handle an UPDATE. Produces one Oaf, stored in CF = [entity]:update_[timestamp]
41
	 * 
42
	 * @param set
43
	 *            the given set.
44
	 * @param agent
45
	 *            the given agent.
46
	 * @param originalId
47
	 *            main entity identifier to be updated (already existing in the information space).
48
	 * @param entityType
49
	 *            one of: result, person, project, datasource, organization {@link eu.dnetlib.data.proto.TypeProtos.Type}.
50
	 * @param updateContent
51
	 *            content to be updated over the original main entity. Oaf#OafEntity#[entity]#id must be consistent with the originalId.
52
	 *            note that: the given trust level has to be [>.9 | NEUTRAL | INFINITE] in order to gain visibility after the merging
53
	 *            process.
54
	 * @return the list of AtomicActions.
55
	 */
56
	public List<AtomicAction> createUpdateActions(final String set,
57
			final Agent agent,
58
			final String originalId,
59
			final Type entityType,
60
			final byte[] updateContent) {
61
		List<AtomicAction> res = Lists.newArrayList();
62

    
63
		res.add(createAtomicAction(set, agent, originalId, entityType.toString(), "update_" + DateUtils.now(), updateContent));
64

    
65
		return res;
66
	}
67

    
68
	public AtomicAction createAtomicAction(final String set,
69
			final Agent agent,
70
			final String targetKey,
71
			final String targetColFamily,
72
			final String targetCol,
73
			final byte[] targetContent) {
74
		AtomicAction action = new AtomicAction(set, agent);
75

    
76
		action.setTargetRowKey(targetKey);
77
		action.setTargetColumnFamily(targetColFamily);
78
		action.setTargetColumn(targetCol);
79
		action.setTargetValue(targetContent);
80

    
81
		return action;
82
	}
83

    
84
	public XsltInfoPackageAction generateInfoPackageAction(final String xsltResource,
85
			final String rawSet,
86
			final Agent agent,
87
			final Operation operation,
88
			final String infoPackage,
89
			final Provenance provenance,
90
			final String nsprefix,
91
			final String trust) throws ActionManagerException {
92

    
93
		final Transformer transformer = prepareXsltTransformer(xsltResource);
94

    
95
		return new XsltInfoPackageAction(rawSet, agent, operation, infoPackage, provenance, nsprefix, trust, transformer, this);
96
	}
97

    
98
	public Transformer prepareXsltTransformer(final String xsltName) throws ActionManagerException {
99
		try {
100
			if (!xslts.containsKey(xsltName)) { throw new ActionManagerException("XSLT " + xsltName + " not found"); }
101

    
102
			final Document doc = new SAXReader().read(xslts.get(xsltName).getInputStream());
103
			if (log.isDebugEnabled()) {
104
				log.debug("using transformer factory: '" + tFactory.getClass().getName() + "'");
105
				log.debug("xslt: \n" + doc.asXML());
106
			}
107
			return tFactory.newTransformer(new DocumentSource(doc));
108
		} catch (Exception e) {
109
			throw new ActionManagerException("Problem with xslt resource " + xsltName, e);
110
		}
111
	}
112

    
113
	public Map<String, Resource> getXslts() {
114
		return xslts;
115
	}
116

    
117
	@Required
118
	public void setXslts(final Map<String, Resource> xslts) {
119
		this.xslts = xslts;
120
	}
121

    
122
}
(2-2/4)