[New] Added Search results for concrete root elements

This commit is contained in:
Robert von Burg 2023-08-25 12:52:26 +02:00
parent 517d9ff0de
commit c4fd605c5b
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
8 changed files with 95 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import static li.strolch.model.StrolchModelConstants.PolicyConstants.PARAM_ORDER
import li.strolch.model.Order;
import li.strolch.model.activity.Activity;
import li.strolch.persistence.api.StrolchTransaction;
/**
* Performs a search for {@link Activity} elements
@ -40,4 +41,9 @@ public class ActivitySearch extends StrolchSearch<Activity> {
super.internal();
return this;
}
@Override
public ActivitySearchResult search(StrolchTransaction tx) {
return new ActivitySearchResult(prepareSearch(tx));
}
}

View File

@ -0,0 +1,11 @@
package li.strolch.search;
import li.strolch.model.activity.Activity;
import java.util.stream.Stream;
public class ActivitySearchResult extends RootElementSearchResult<Activity> {
public ActivitySearchResult(Stream<Activity> stream) {
super(stream);
}
}

View File

@ -1,12 +1,13 @@
package li.strolch.search;
import static java.util.stream.Collectors.toList;
import li.strolch.model.Order;
import li.strolch.model.State;
import li.strolch.persistence.api.StrolchTransaction;
import java.util.Arrays;
import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.State;
import static java.util.stream.Collectors.toList;
/**
* Performs a search of {@link Order} elements
@ -61,4 +62,9 @@ public class OrderSearch extends StrolchSearch<Order> {
super.internal();
return this;
}
@Override
public OrderSearchResult search(StrolchTransaction tx) {
return new OrderSearchResult(prepareSearch(tx));
}
}

View File

@ -0,0 +1,37 @@
package li.strolch.search;
import li.strolch.model.Order;
import li.strolch.model.StrolchElement;
import java.util.Comparator;
import java.util.stream.Stream;
public class OrderSearchResult extends RootElementSearchResult<Order> {
public OrderSearchResult(Stream<Order> stream) {
super(stream);
}
/**
* Appends a comparator to the stream of elements to compare by date
*
* @return this for chaining
*/
public OrderSearchResult orderByDate() {
return orderByDate(false);
}
/**
* Appends a comparator to the stream of elements to compare by date
*
* @param reversed flag to reverse the comparison
*
* @return this for chaining
*/
public OrderSearchResult orderByDate(boolean reversed) {
Comparator<Order> comparator = Comparator.comparing(Order::getDate);
if (reversed)
comparator = comparator.reversed();
this.stream = this.stream.sorted(comparator);
return this;
}
}

View File

@ -1,6 +1,7 @@
package li.strolch.search;
import li.strolch.model.Resource;
import li.strolch.persistence.api.StrolchTransaction;
/**
* Performs a search for {@link Resource} elements
@ -31,4 +32,9 @@ public class ResourceSearch extends StrolchSearch<Resource> {
super.internal();
return this;
}
@Override
public ResourceSearchResult search(StrolchTransaction tx) {
return new ResourceSearchResult(prepareSearch(tx));
}
}

View File

@ -0,0 +1,11 @@
package li.strolch.search;
import li.strolch.model.Resource;
import java.util.stream.Stream;
public class ResourceSearchResult extends RootElementSearchResult<Resource> {
public ResourceSearchResult(Stream<Resource> stream) {
super(stream);
}
}

View File

@ -6,6 +6,7 @@ import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.activity.Activity;
import li.strolch.persistence.api.StrolchTransaction;
/**
* Performs a search for any kind of root element, allowing to mix {@link Resource}, {@link Order} and {@link Activity}
@ -33,4 +34,9 @@ public class RootElementSearch extends StrolchSearch<StrolchRootElement> {
return this;
}
@Override
public RootElementSearchResult<StrolchRootElement> search(StrolchTransaction tx) {
return new RootElementSearchResult<>(prepareSearch(tx));
}
}

View File

@ -43,8 +43,7 @@ public abstract class StrolchSearch<T extends StrolchRootElement>
/**
* Used to configure the navigator, i.e. which <code>type</code> of root elements are to be queried
*
* @param types
* the types of elements to search
* @param types the types of elements to search
*
* @return this for chaining
*/
@ -60,8 +59,7 @@ public abstract class StrolchSearch<T extends StrolchRootElement>
/**
* Adds the given {@link SearchExpression} to the current search
*
* @param expression
* the {@link SearchExpression} to add to this search
* @param expression the {@link SearchExpression} to add to this search
*
* @return this for chaining
*/
@ -87,12 +85,13 @@ public abstract class StrolchSearch<T extends StrolchRootElement>
/**
* Performs the actual search, by first validating the privilege context
*
* @param tx
* the TX on which to perform the search
* @param tx the TX on which to perform the search
*
* @return the search result
*/
public RootElementSearchResult<T> search(StrolchTransaction tx) {
public abstract RootElementSearchResult<T> search(StrolchTransaction tx);
protected Stream<T> prepareSearch(StrolchTransaction tx) {
try {
tx.getPrivilegeContext().validateAction(this);
} catch (AccessDeniedException e) {
@ -101,8 +100,7 @@ public abstract class StrolchSearch<T extends StrolchRootElement>
if (tx.getContainer().hasComponent(OperationsLog.class)) {
String realmName = tx.getRealmName();
String searchName = this.privilegeValue.equals(INTERNAL) ?
(getClass().getName() + " (INTERNAL)") :
String searchName = this.privilegeValue.equals(INTERNAL) ? (getClass().getName() + " (INTERNAL)") :
this.privilegeValue;
LogMessage logMessage = new LogMessage(realmName, username,
Locator.valueOf(AGENT, PrivilegeHandler.class.getSimpleName(), getPrivilegeName(), searchName),
@ -114,8 +112,7 @@ public abstract class StrolchSearch<T extends StrolchRootElement>
operationsLog.addMessage(logMessage);
}
String searchName = this.privilegeValue.equals(INTERNAL) ?
(getClass().getSimpleName() + " (INTERNAL)") :
String searchName = this.privilegeValue.equals(INTERNAL) ? (getClass().getSimpleName() + " (INTERNAL)") :
getClass().getSimpleName();
I18nMessage i18n = new I18nMessage(
ResourceBundle.getBundle("strolch-agent", tx.getCertificate().getLocale()),
@ -134,8 +131,7 @@ public abstract class StrolchSearch<T extends StrolchRootElement>
if (this.expression != null)
stream = stream.filter(e -> this.expression.matches(e));
return new RootElementSearchResult<>(stream);
return stream;
}
/**