Project

General

Profile

1
package eu.dnetlib.data.objectstore;
2

    
3
import java.util.List;
4

    
5
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
6
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
7
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
8
import eu.dnetlib.rmi.enabling.ISLookUpException;
9
import eu.dnetlib.rmi.enabling.ISLookUpService;
10
import org.apache.commons.lang3.BooleanUtils;
11
import org.apache.commons.logging.Log;
12
import org.apache.commons.logging.LogFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14

    
15
/**
16
 * The Class CreateObjectStoreAction is responsible to execute the blacboard action of type CREATE.
17
 */
18
public class CreateObjectStoreAction extends AbstractObjectStoreAction {
19

    
20
	private static final Log log = LogFactory.getLog(CreateObjectStoreAction.class);
21

    
22
	@Autowired
23
	private UniqueServiceLocator locator;
24

    
25

    
26
	/**
27
	 * The profile creator.
28
	 */
29
	private ObjectStoreProfileCreator profileCreator;
30

    
31
	/**
32
	 * Gets the profile creator.
33
	 *
34
	 * @return the profile creator
35
	 */
36
	public ObjectStoreProfileCreator getProfileCreator() {
37
		return profileCreator;
38
	}
39

    
40
	/**
41
	 * Sets the profile creator.
42
	 *
43
	 * @param profileCreator the new profile creator
44
	 */
45
	public void setProfileCreator(final ObjectStoreProfileCreator profileCreator) {
46
		this.profileCreator = profileCreator;
47
	}
48

    
49
	@Override
50
	protected void executeAsync(final BlackboardServerHandler handler, final BlackboardJob job) {
51
		try {
52
			final String interpretation = job.getParameters().get("interpretation");
53
			final String basePath = job.getParameters().get("basePath");
54
			final boolean createOnlyIfMissing = BooleanUtils.toBoolean(job.getParameters().get("createOnlyIfMissing"));
55
			if (createOnlyIfMissing) {
56
				final String objID = findObjectStoreByInterpretation(interpretation);
57
				if (objID != null) {
58
					job.getParameters().put("objectStoreId", objID);
59
					completeWithSuccess(handler, job);
60
					return;
61
				}
62
			}
63

    
64
			final String objID = profileCreator.registerProfile(interpretation);
65
			getDao().createObjectStore(objID, interpretation, basePath);
66
			job.getParameters().put("objectStoreId", objID);
67
			completeWithSuccess(handler, job);
68
		} catch (Exception e) {
69
			completeWithFail(handler, job, e);
70
		}
71

    
72
	}
73

    
74
	private String findObjectStoreByInterpretation(final String interpretation) {
75
		final ISLookUpService service = locator.getService(ISLookUpService.class);
76
		final String query =
77
				"for $x in collection('/db/DRIVER/ObjectStoreDSResources/ObjectStoreDSResourceType') where $x//OBJECTSTORE_INTERPRETATION='%s' return $x//RESOURCE_IDENTIFIER/@value/string()";
78
		try {
79
			final List<String> strings = service.quickSearchProfile(String.format(query, interpretation));
80
			if (strings != null && strings.size() > 0)
81
				return strings.get(0);
82
		} catch (ISLookUpException e) {
83
			log.error("error on executing query", e);
84

    
85
		}
86
		return null;
87

    
88
	}
89
}
(2-2/21)