[New] added tx.get*()-methods with assertExists boolean

- now a user can always use that flag to guarantee the element exists
This commit is contained in:
Robert von Burg 2015-01-23 12:06:15 +01:00
parent 120ff0ace9
commit be6f1d170f
6 changed files with 97 additions and 150 deletions

View File

@ -65,7 +65,7 @@ public interface ElementMap<T extends StrolchRootElement> {
public T getBy(StrolchTransaction tx, String type, String id);
/**
* Returns the Element which is referenced by the given {@link StringParameter}. A reference {@link Parameter} must
* Returns the element which is referenced by the given {@link StringParameter}. A reference {@link Parameter} must
* have its interpretation set to the element type being referenced e.g. s
* {@link StrolchConstants#INTERPRETATION_ORDER_REF} and the UOM must be set to the element's type and the value is
* the id of the element
@ -74,12 +74,15 @@ public interface ElementMap<T extends StrolchRootElement> {
* the {@link StrolchTransaction} instance
* @param refP
* the {@link StringParameter} which references an element
* @param assertExists
* if true, and element does not exist, then a {@link StrolchException} is thrown
*
* @return the element found, or null if it does not exist
*
* @throws StrolchException
* if the {@link StringParameter} is not a properly configured as a reference parameter
*/
public T getBy(StrolchTransaction tx, StringParameter refP) throws StrolchException;
public T getBy(StrolchTransaction tx, StringParameter refP, boolean assertExists) throws StrolchException;
/**
* Returns all elements which are referenced by the given {@link StringListParameter}. A reference {@link Parameter}
@ -91,6 +94,8 @@ public interface ElementMap<T extends StrolchRootElement> {
* the {@link StrolchTransaction} instance
* @param refP
* the {@link StringListParameter} which references an element
* @param assertExists
* if true, and element does not exist, then a {@link StrolchException} is thrown
*
* @return the list of elements found, or the empty list if they do not exist. <b>Note:</b> Any missing elements are
* not returned!
@ -98,7 +103,7 @@ public interface ElementMap<T extends StrolchRootElement> {
* @throws StrolchException
* if the {@link StringParameter} is not a properly configured as a reference parameter
*/
public List<T> getBy(StrolchTransaction tx, StringListParameter refP) throws StrolchException;
public List<T> getBy(StrolchTransaction tx, StringListParameter refP, boolean assertExists) throws StrolchException;
public List<T> getAllElements(StrolchTransaction tx);

View File

@ -105,57 +105,26 @@ public class AuditingElementMapFacade<T extends StrolchRootElement> implements E
return this.deletedAllByType;
}
/**
* @param tx
* @param type
* @return
* @see li.strolch.agent.api.ElementMap#hasType(li.strolch.persistence.api.StrolchTransaction, java.lang.String)
*/
@Override
public boolean hasType(StrolchTransaction tx, String type) {
return this.elementMap.hasType(tx, type);
}
/**
* @param tx
* @param type
* @param id
* @return
* @see li.strolch.agent.api.ElementMap#hasElement(li.strolch.persistence.api.StrolchTransaction, java.lang.String,
* java.lang.String)
*/
@Override
public boolean hasElement(StrolchTransaction tx, String type, String id) {
return this.elementMap.hasElement(tx, type, id);
}
/**
* @param tx
* @return
* @see li.strolch.agent.api.ElementMap#querySize(li.strolch.persistence.api.StrolchTransaction)
*/
@Override
public long querySize(StrolchTransaction tx) {
return this.elementMap.querySize(tx);
}
/**
* @param tx
* @param type
* @return
* @see li.strolch.agent.api.ElementMap#querySize(li.strolch.persistence.api.StrolchTransaction, java.lang.String)
*/
@Override
public long querySize(StrolchTransaction tx, String type) {
return this.elementMap.querySize(tx, type);
}
/**
* @param tx
* @param type
* @return
* @see li.strolch.agent.api.ElementMap#getTemplate(li.strolch.persistence.api.StrolchTransaction, java.lang.String)
*/
@Override
public T getTemplate(StrolchTransaction tx, String type) {
T template = this.elementMap.getTemplate(tx, type);
@ -164,14 +133,6 @@ public class AuditingElementMapFacade<T extends StrolchRootElement> implements E
return template;
}
/**
* @param tx
* @param type
* @param id
* @return
* @see li.strolch.agent.api.ElementMap#getBy(li.strolch.persistence.api.StrolchTransaction, java.lang.String,
* java.lang.String)
*/
@Override
public T getBy(StrolchTransaction tx, String type, String id) {
T element = this.elementMap.getBy(tx, type, id);
@ -180,35 +141,22 @@ public class AuditingElementMapFacade<T extends StrolchRootElement> implements E
return element;
}
/**
* @param tx
* @param refP
* @return
* @throws StrolchException
* @see li.strolch.agent.api.ElementMap#getBy(li.strolch.persistence.api.StrolchTransaction,
* li.strolch.model.parameter.StringParameter)
*/
@Override
public T getBy(StrolchTransaction tx, StringParameter refP) throws StrolchException {
T element = this.elementMap.getBy(tx, refP);
public T getBy(StrolchTransaction tx, StringParameter refP, boolean assertExists) throws StrolchException {
T element = this.elementMap.getBy(tx, refP, assertExists);
if (this.observeAccessReads)
this.read.add(element);
return element;
}
@Override
public List<T> getBy(StrolchTransaction tx, StringListParameter refP) throws StrolchException {
List<T> elements = this.elementMap.getBy(tx, refP);
public List<T> getBy(StrolchTransaction tx, StringListParameter refP, boolean assertExists) throws StrolchException {
List<T> elements = this.elementMap.getBy(tx, refP, assertExists);
if (this.observeAccessReads)
this.read.addAll(elements);
return elements;
}
/**
* @param tx
* @return
* @see li.strolch.agent.api.ElementMap#getAllElements(li.strolch.persistence.api.StrolchTransaction)
*/
@Override
public List<T> getAllElements(StrolchTransaction tx) {
List<T> elements = this.elementMap.getAllElements(tx);
@ -217,13 +165,6 @@ public class AuditingElementMapFacade<T extends StrolchRootElement> implements E
return elements;
}
/**
* @param tx
* @param type
* @return
* @see li.strolch.agent.api.ElementMap#getElementsBy(li.strolch.persistence.api.StrolchTransaction,
* java.lang.String)
*/
@Override
public List<T> getElementsBy(StrolchTransaction tx, String type) {
List<T> elements = this.elementMap.getElementsBy(tx, type);
@ -232,67 +173,33 @@ public class AuditingElementMapFacade<T extends StrolchRootElement> implements E
return elements;
}
/**
* @param tx
* @return
* @see li.strolch.agent.api.ElementMap#getTypes(li.strolch.persistence.api.StrolchTransaction)
*/
@Override
public Set<String> getTypes(StrolchTransaction tx) {
return this.elementMap.getTypes(tx);
}
/**
* @param tx
* @return
* @see li.strolch.agent.api.ElementMap#getAllKeys(li.strolch.persistence.api.StrolchTransaction)
*/
@Override
public Set<String> getAllKeys(StrolchTransaction tx) {
return this.elementMap.getAllKeys(tx);
}
/**
* @param tx
* @param type
* @return
* @see li.strolch.agent.api.ElementMap#getKeysBy(li.strolch.persistence.api.StrolchTransaction, java.lang.String)
*/
@Override
public Set<String> getKeysBy(StrolchTransaction tx, String type) {
return this.elementMap.getKeysBy(tx, type);
}
/**
* @param tx
* @param element
* @see li.strolch.agent.api.ElementMap#add(li.strolch.persistence.api.StrolchTransaction,
* li.strolch.model.StrolchRootElement)
*/
@Override
public void add(StrolchTransaction tx, T element) {
this.elementMap.add(tx, element);
this.created.add(element);
}
/**
* @param tx
* @param elements
* @see li.strolch.agent.api.ElementMap#addAll(li.strolch.persistence.api.StrolchTransaction, java.util.List)
*/
@Override
public void addAll(StrolchTransaction tx, List<T> elements) {
this.elementMap.addAll(tx, elements);
this.created.addAll(elements);
}
/**
* @param tx
* @param element
* @return
* @see li.strolch.agent.api.ElementMap#update(li.strolch.persistence.api.StrolchTransaction,
* li.strolch.model.StrolchRootElement)
*/
@Override
public T update(StrolchTransaction tx, T element) {
T replaced = this.elementMap.update(tx, element);
@ -301,12 +208,6 @@ public class AuditingElementMapFacade<T extends StrolchRootElement> implements E
}
/**
* @param tx
* @param elements
* @return
* @see li.strolch.agent.api.ElementMap#updateAll(li.strolch.persistence.api.StrolchTransaction, java.util.List)
*/
@Override
public List<T> updateAll(StrolchTransaction tx, List<T> elements) {
List<T> replaced = this.elementMap.updateAll(tx, elements);
@ -314,34 +215,18 @@ public class AuditingElementMapFacade<T extends StrolchRootElement> implements E
return replaced;
}
/**
* @param tx
* @param element
* @see li.strolch.agent.api.ElementMap#remove(li.strolch.persistence.api.StrolchTransaction,
* li.strolch.model.StrolchRootElement)
*/
@Override
public void remove(StrolchTransaction tx, T element) {
this.elementMap.remove(tx, element);
this.deleted.add(element);
}
/**
* @param tx
* @param elements
* @see li.strolch.agent.api.ElementMap#removeAll(li.strolch.persistence.api.StrolchTransaction, java.util.List)
*/
@Override
public void removeAll(StrolchTransaction tx, List<T> elements) {
this.elementMap.removeAll(tx, elements);
this.deleted.addAll(elements);
}
/**
* @param tx
* @return
* @see li.strolch.agent.api.ElementMap#removeAll(li.strolch.persistence.api.StrolchTransaction)
*/
@Override
public long removeAll(StrolchTransaction tx) {
long removed = this.elementMap.removeAll(tx);
@ -349,12 +234,6 @@ public class AuditingElementMapFacade<T extends StrolchRootElement> implements E
return removed;
}
/**
* @param tx
* @param type
* @return
* @see li.strolch.agent.api.ElementMap#removeAllBy(li.strolch.persistence.api.StrolchTransaction, java.lang.String)
*/
@Override
public long removeAllBy(StrolchTransaction tx, String type) {
long removed = this.elementMap.removeAllBy(tx, type);

View File

@ -107,15 +107,21 @@ public abstract class CachedElementMap<T extends StrolchRootElement> implements
protected abstract void assertIsRefParam(Parameter<?> refP);
@Override
public T getBy(StrolchTransaction tx, StringParameter refP) throws StrolchException {
public T getBy(StrolchTransaction tx, StringParameter refP, boolean assertExists) throws StrolchException {
assertIsRefParam(refP);
String type = refP.getUom();
String id = refP.getValue();
return getBy(tx, type, id);
T element = getBy(tx, type, id);
if (assertExists && element == null) {
String msg = "The element for refP {0} with id {1} does not exist!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, refP.getLocator(), id);
throw new StrolchException(msg);
}
return element;
}
@Override
public List<T> getBy(StrolchTransaction tx, StringListParameter refP) throws StrolchException {
public List<T> getBy(StrolchTransaction tx, StringListParameter refP, boolean assertExists) throws StrolchException {
assertIsRefParam(refP);
List<T> elements = new ArrayList<>();
@ -124,8 +130,15 @@ public abstract class CachedElementMap<T extends StrolchRootElement> implements
for (String id : ids) {
T element = getBy(tx, type, id);
if (element != null)
if (element != null) {
elements.add(element);
} else if (assertExists) {
if (assertExists && element == null) {
String msg = "The element for refP {0} with id {1} does not exist!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, refP.getLocator(), id);
throw new StrolchException(msg);
}
}
}
return elements;

View File

@ -1,5 +1,6 @@
package li.strolch.agent.impl;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@ -56,15 +57,21 @@ public abstract class TransactionalElementMap<T extends StrolchRootElement> impl
protected abstract void assertIsRefParam(Parameter<?> refP);
@Override
public T getBy(StrolchTransaction tx, StringParameter refP) throws StrolchException {
public T getBy(StrolchTransaction tx, StringParameter refP, boolean assertExists) throws StrolchException {
assertIsRefParam(refP);
String type = refP.getUom();
String id = refP.getValue();
return getBy(tx, type, id);
T element = getBy(tx, type, id);
if (assertExists && element == null) {
String msg = "The element for refP {0} with id {1} does not exist!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, refP.getLocator(), id);
throw new StrolchException(msg);
}
return element;
}
@Override
public List<T> getBy(StrolchTransaction tx, StringListParameter refP) throws StrolchException {
public List<T> getBy(StrolchTransaction tx, StringListParameter refP, boolean assertExists) throws StrolchException {
assertIsRefParam(refP);
List<T> elements = new ArrayList<>();
@ -73,8 +80,15 @@ public abstract class TransactionalElementMap<T extends StrolchRootElement> impl
for (String id : ids) {
T element = getBy(tx, type, id);
if (element != null)
if (element != null) {
elements.add(element);
} else if (assertExists) {
if (assertExists && element == null) {
String msg = "The element for refP {0} with id {1} does not exist!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, refP.getLocator(), id);
throw new StrolchException(msg);
}
}
}
return elements;

View File

@ -395,17 +395,17 @@ public abstract class AbstractTransaction implements StrolchTransaction {
@Override
public Order getOrderBy(StringParameter refP, boolean assertExists) throws StrolchException {
Order order = getOrderMap().getBy(this, refP);
if (assertExists && order == null) {
String msg = "No Order exists with the id {0} with type {1} for refP {2}";
throw new StrolchException(MessageFormat.format(msg, refP.getValue(), refP.getUom(), refP.getLocator()));
}
return order;
return getOrderMap().getBy(this, refP, assertExists);
}
@Override
public List<Order> getOrdersBy(StringListParameter refP) throws StrolchException {
return getOrderMap().getBy(this, refP);
return getOrderMap().getBy(this, refP, false);
}
@Override
public List<Order> getOrdersBy(StringListParameter refP, boolean assertExists) throws StrolchException {
return getOrderMap().getBy(this, refP, assertExists);
}
@Override
@ -430,17 +430,17 @@ public abstract class AbstractTransaction implements StrolchTransaction {
@Override
public Resource getResourceBy(StringParameter refP, boolean assertExists) throws StrolchException {
Resource resource = getResourceMap().getBy(this, refP);
if (assertExists && resource == null) {
String msg = "No Resource exists with the id {0} with type {1} for refP {2}";
throw new StrolchException(MessageFormat.format(msg, refP.getValue(), refP.getUom(), refP.getLocator()));
}
return resource;
return getResourceMap().getBy(this, refP, assertExists);
}
@Override
public List<Resource> getResourcesBy(StringListParameter refP) throws StrolchException {
return getResourceMap().getBy(this, refP);
return getResourceMap().getBy(this, refP, false);
}
@Override
public List<Resource> getResourcesBy(StringListParameter refP, boolean assertExists) throws StrolchException {
return getResourceMap().getBy(this, refP, assertExists);
}
@Override

View File

@ -580,6 +580,24 @@ public interface StrolchTransaction extends AutoCloseable {
*/
public List<Resource> getResourcesBy(StringListParameter refP) throws StrolchException;
/**
* Returns all {@link Resource Resources} which are referenced by the given {@link StringListParameter}. A reference
* {@link Parameter} must have its interpretation set to {@link StrolchConstants#INTERPRETATION_RESOURCE_REF} and
* the UOM must be set to the resource's type and the value is the id of the resource
*
* @param refP
* the {@link StringListParameter} which references a list of {@link Resource Resources}
* @param assertExists
* if true, and resource does not exist, then a {@link StrolchException} is thrown
*
* @return the resources referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any
* missing resources are not returned!
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Resource> getResourcesBy(StringListParameter refP, boolean assertExists) throws StrolchException;
/**
* Returns the {@link Order} with the given type and id, or null if it does not exist
*
@ -657,4 +675,22 @@ public interface StrolchTransaction extends AutoCloseable {
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Order> getOrdersBy(StringListParameter refP) throws StrolchException;
/**
* Returns all {@link Order Orders} which are referenced by the given {@link StringListParameter}. A reference
* {@link Parameter} must have its interpretation set to {@link StrolchConstants#INTERPRETATION_ORDER_REF} and the
* UOM must be set to the order's type and the value is the id of the order
*
* @param refP
* the {@link StringListParameter} which references a list of {@link Order Orders}
* @param assertExists
* if true, and order does not exist, then a {@link StrolchException} is thrown
*
* @return the orders referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any missing
* orders are not returned!
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Order> getOrdersBy(StringListParameter refP, boolean assertExists) throws StrolchException;
}