diff --git a/src/main/java/org/gcube/documentstore/exception/InvalidValueException.java b/src/main/java/org/gcube/documentstore/exception/InvalidValueException.java new file mode 100644 index 0000000..473423d --- /dev/null +++ b/src/main/java/org/gcube/documentstore/exception/InvalidValueException.java @@ -0,0 +1,13 @@ +package org.gcube.documentstore.exception; + +public class InvalidValueException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 2449589259703195853L; + + public InvalidValueException() { + super(); + } +} diff --git a/src/main/java/org/gcube/documentstore/records/Record.java b/src/main/java/org/gcube/documentstore/records/Record.java new file mode 100644 index 0000000..42bad89 --- /dev/null +++ b/src/main/java/org/gcube/documentstore/records/Record.java @@ -0,0 +1,119 @@ +/** + * + */ +package org.gcube.documentstore.records; + +import java.io.Serializable; +import java.util.Calendar; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; + +import org.gcube.documentstore.exception.InvalidValueException; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public interface Record extends Comparable, Serializable { + /** + * @return a Set containing the keys of required fields + * The returned Set MUST be a copy of the internal representation. + * Any modification to the returned Set MUST not affect the object + */ + public Set getRequiredFields(); + + /** + * @return a Set containing the keys of computed fields + * The returned Set MUST be a copy of the internal representation. + * Any modification to the returned Set MUST not affect the object + */ + public Set getComputedFields(); + + /** + * @return Querable Keys as SortedSet + * @throws Exception + */ + public SortedSet getQuerableKeys() throws Exception; + + /** + * Return the {@link Record} Type + * @return {@link Record} Type + */ + public String getRecordType(); + + + /** + * Return the unique id for this {@link Record} + * @return {@link Record} Unique ID + */ + public String getId(); + + /** + * The ID SHOULD be automatically created by the implementation Class. + * Set the ID only if you really know what you are going to do. + * Set the unique id for this {@link Record} + * @param id Unique ID + * @throws InvalidValueException + */ + public void setId(String id) throws InvalidValueException; + + /** + * Return the instant when this {@link Record} was created. + * @return the creation time for this {@link Record} + */ + public Calendar getCreationTime(); + + /** + * The CreationTime is automatically created by the implementation Class. + * Set the CreationTime only if you really know what you are going to do. + * Set the instant when this {@link Record} was created. + * @param creationTime Creation Time + * @throws InvalidValueException + */ + public void setCreationTime(Calendar creationTime) throws InvalidValueException; + + /** + * Return all resource-specific properties. The returned Map is a copy of + * the internal representation. Any modification to the returned Map MUST + * not affect the object + * @return a Map containing the properties + */ + public Map getResourceProperties(); + + /** + * Set all resource-specific properties, replacing existing ones + */ + public void setResourceProperties(Map resourceSpecificProperties) throws InvalidValueException; + + /** + * Return the value of the given resource property. + * @param key the key of the requested property + * @return the value of the given resource property + */ + public Serializable getResourceProperty(String key); + + /** + * Set the value of the given resource property. + * If the key has the value of one of the predefined property, the value + * is validated. + * @param key the key of the requested property + * @param value the value of the given resource property + */ + public void setResourceProperty(String key, Serializable value) throws InvalidValueException; + + /** + * Remove a property from Record. + * This API is intended for intern use only. + * @param key the key of the requested property to remove + */ + public void removeResourceProperty(String key); + + /** + * Validate the Resource Record. + * The validation check if all the Required Field are set and has valid + * value. + * @throws InvalidValueException + */ + public void validate() throws InvalidValueException; + +} diff --git a/src/main/java/org/gcube/documentstore/records/implementation/AbstractRecord.java b/src/main/java/org/gcube/documentstore/records/implementation/AbstractRecord.java new file mode 100644 index 0000000..3f28445 --- /dev/null +++ b/src/main/java/org/gcube/documentstore/records/implementation/AbstractRecord.java @@ -0,0 +1,152 @@ +/** + * + */ +package org.gcube.documentstore.records.implementation; + +import java.io.Serializable; +import java.util.Calendar; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.UUID; + +import org.gcube.documentstore.exception.InvalidValueException; +import org.gcube.documentstore.records.Record; + + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public abstract class AbstractRecord implements Record { + + /** + * + */ + private static final long serialVersionUID = 8746066891880733502L; + + @Override + public SortedSet getQuerableKeys() throws Exception { + return new TreeSet<>(); + } + + public AbstractRecord() { + } + + public AbstractRecord(Map properties) throws InvalidValueException { + } + + /** + * {@inheritDoc} + */ + @Override + public Set getRequiredFields() { + return new HashSet(); + } + + /** + * {@inheritDoc} + */ + @Override + public Set getComputedFields() { + return new HashSet(); + } + + public Set getAggregatedFields() { + return new HashSet(); + } + + /** + * {@inheritDoc} + */ + @Override + public String getId() { + return UUID.randomUUID().toString(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setId(String id) throws InvalidValueException { + + } + + public static Calendar timestampToCalendar(long millis) { + return Calendar.getInstance(); + } + + /** + * {@inheritDoc} + */ + @Override + public Calendar getCreationTime() { + return Calendar.getInstance(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setCreationTime(Calendar creationTime) throws InvalidValueException { + } + + /** + * {@inheritDoc} + */ + @Override + public Map getResourceProperties() { + return new HashMap(); + } + + /** + * {@inheritDoc} + */ + @Override + public void setResourceProperties(Map properties) throws InvalidValueException { + + } + + /** + * {@inheritDoc} + */ + @Override + public Serializable getResourceProperty(String key) { + return "TEST"; + } + + @Override + public void removeResourceProperty(String key) { + } + + /** + * {@inheritDoc} + */ + @Override + public void setResourceProperty(String key, Serializable value) throws InvalidValueException { + + } + + + /** + * {@inheritDoc} + */ + @Override + public void validate() throws InvalidValueException { + + } + + @Override + public String toString() { + return "{}"; + } + + + @Override + public int compareTo(Record record) { + return 0; + } + +}