Project

General

Profile

1
package eu.dnetlib.enabling.tools;
2

    
3
import java.io.BufferedWriter;
4
import java.io.File;
5
import java.io.FileWriter;
6
import java.io.IOException;
7

    
8
import eu.dnetlib.enabling.is.sn.rmi.ISSNService;
9
import eu.dnetlib.enabling.is.store.rmi.ISStoreException;
10
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;
11

    
12
/**
13
 * 
14
 * TODO: document. TODO: Too low level, why the interfacing the Store and not the Registry ?
15
 * 
16
 * @author michele
17
 * 
18
 */
19
public class ResourceType {
20

    
21
	/**
22
	 * xmldb collection where resource types are stored.
23
	 */
24
	public static final String RESOURCE_TYPES = "DRIVERResourceTypes";
25

    
26
	/**
27
	 * resource type. (Including kind?)
28
	 */
29
	private String resourceType; // NOPMD
30

    
31
	/**
32
	 * resource schema. (Body?)
33
	 */
34
	private String resourceSchema; // NOPMD
35
	/**
36
	 * file where a copy of the schema is stored locally TODO: why ?
37
	 */
38
	private File fileSchema;
39

    
40
	/**
41
	 * instanciate an already registered resource type, fetching it from the store.
42
	 * 
43
	 * @param resourceType
44
	 *            resourceType name
45
	 * @param store
46
	 *            store service where the type is stored
47
	 * @param basedir
48
	 *            base dir where a local copy is held (why?)
49
	 * @throws ISStoreException
50
	 *             happens
51
	 */
52
	public ResourceType(final String resourceType, final ISStoreService store, final String basedir) throws ISStoreException {
53
		this(resourceType, store.getXML(resourceType, RESOURCE_TYPES), basedir);
54
	}
55

    
56
	/**
57
	 * construct a resource type.
58
	 * 
59
	 * @param resourceType
60
	 *            resourceType name
61
	 * @param resourceSchema
62
	 *            resourceSchema xsd body
63
	 * @param basedir
64
	 *            base directory of some local copy (?)
65
	 * @throws ISStoreException
66
	 *             happens
67
	 */
68
	public ResourceType(final String resourceType, final String resourceSchema, final String basedir) throws ISStoreException {
69
		this.resourceType = resourceType;
70
		this.resourceSchema = resourceSchema;
71
		if (basedir != null)
72
			this.fileSchema = saveSchemaAsFile(basedir);
73
	}
74

    
75
	/**
76
	 * stores the schema on the xmldb.
77
	 * 
78
	 * @param store
79
	 *            store service
80
	 * @throws ISStoreException
81
	 *             happens
82
	 */
83
	public void store(final ISStoreService store) throws ISStoreException {
84
		store.insertXML(resourceType, RESOURCE_TYPES, resourceSchema);
85
	}
86

    
87
	/**
88
	 * updates the in memory resource instance from the store.
89
	 * 
90
	 * @param store store service
91
	 * @throws ISStoreException happens
92
	 */
93
	public void update(final ISStoreService store) throws ISStoreException {
94
		store.updateXML(resourceType, RESOURCE_TYPES, resourceSchema);
95
	}
96

    
97
	/**
98
	 * save the schema to a file.
99
	 * 
100
	 * @param basedir
101
	 *            base directory
102
	 * @return the newly created file
103
	 * @throws ISStoreException
104
	 *             happens, even if the file cannot be created.
105
	 */
106
	private File saveSchemaAsFile(final String basedir) throws ISStoreException {
107
		fileSchema = new File(basedir + "/" + resourceType);
108
		if (!fileSchema.exists()) {
109
			try {
110
				final BufferedWriter out = new BufferedWriter(new FileWriter(fileSchema));
111
				out.write(resourceSchema);
112
				out.close();
113
			} catch (IOException e) {
114
				throw new ISStoreException("Error saving file", e);
115
			}
116
		}
117
		return fileSchema;
118
	}
119

    
120
	/**
121
	 * it says "delete and notify" but it only deletes the schema from the filesystem and from the store.
122
	 * 
123
	 * TODO: document and possibly refactor.
124
	 * 
125
	 * @param store
126
	 *            store service
127
	 * @param issn
128
	 *            ISSN service
129
	 * @throws ISStoreException
130
	 *             happens
131
	 */
132
	public void deleteAndNotify(final ISStoreService store, final ISSNService issn) throws ISStoreException {
133
		fileSchema.delete();
134
		store.deleteXML(resourceType, RESOURCE_TYPES);
135
	}
136

    
137
	public String getResourceType() {
138
		return resourceType;
139
	}
140

    
141
	public void setResourceType(final String resourceType) {
142
		this.resourceType = resourceType;
143
	}
144

    
145
	public String getResourceSchema() {
146
		return resourceSchema;
147
	}
148

    
149
	public void setResourceSchema(final String resourceSchema) {
150
		this.resourceSchema = resourceSchema;
151
	}
152

    
153
	public File getFileSchema() {
154
		return fileSchema;
155
	}
156

    
157
	public void setFileSchema(final File fileSchema) {
158
		this.fileSchema = fileSchema;
159
	}
160

    
161
}
(3-3/4)