[New] Added bulk change commands for all objects

This commit is contained in:
Robert von Burg 2021-07-16 14:25:23 +02:00
parent 72d84d1ea5
commit bff8658c22
9 changed files with 816 additions and 0 deletions

View File

@ -0,0 +1,89 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.activity.Activity;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class AddActivitiesCommand extends Command {
private List<Activity> activities = new ArrayList<>();
private final List<Activity> added = new ArrayList<>();
public AddActivitiesCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param activities
* the activities to set for adding
*/
public void setActivities(List<Activity> activities) {
this.activities = activities;
}
/**
* @param activity
* the activity to add for adding
*/
public void addActivitiy(Activity activity) {
this.activities.add(activity);
}
@Override
public void validate() {
DBC.PRE.assertNotNull("Activities may not be null!", this.activities);
}
@Override
public void doCommand() {
this.activities.forEach(activity -> tx().lock(activity));
ActivityMap activityMap = tx().getActivityMap();
this.activities.forEach(activity -> {
if (activityMap.hasElement(tx(), activity.getType(), activity.getId())) {
String msg = MessageFormat.format("The Activity {0} already exists!", activity.getLocator());
throw new StrolchException(msg);
}
activityMap.add(tx(), activity);
this.added.add(activity);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.added.isEmpty()) {
this.added.forEach(activity -> {
ActivityMap activityMap = tx().getActivityMap();
if (activityMap.hasElement(tx(), activity.getType(), activity.getId()))
activityMap.remove(tx(), activity);
});
}
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class AddOrdersCommand extends Command {
private List<Order> orders = new ArrayList<>();
private final List<Order> added = new ArrayList<>();
public AddOrdersCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param orders
* the orders to set for adding
*/
public void setOrders(List<Order> orders) {
this.orders = orders;
}
/**
* @param resource
* the resource to add for adding
*/
public void addOrder(Order resource) {
this.orders.add(resource);
}
@Override
public void validate() {
DBC.PRE.assertNotNull("Orders may not be null!", this.orders);
}
@Override
public void doCommand() {
this.orders.forEach(resource -> tx().lock(resource));
OrderMap resourceMap = tx().getOrderMap();
this.orders.forEach(resource -> {
if (resourceMap.hasElement(tx(), resource.getType(), resource.getId())) {
String msg = MessageFormat.format("The Order {0} already exists!", resource.getLocator());
throw new StrolchException(msg);
}
resourceMap.add(tx(), resource);
this.added.add(resource);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.added.isEmpty()) {
this.added.forEach(resource -> {
OrderMap resourceMap = tx().getOrderMap();
if (resourceMap.hasElement(tx(), resource.getType(), resource.getId()))
resourceMap.remove(tx(), resource);
});
}
}
}

View File

@ -0,0 +1,89 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Resource;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class AddResourcesCommand extends Command {
private List<Resource> resources = new ArrayList<>();
private final List<Resource> added = new ArrayList<>();
public AddResourcesCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param resources
* the resources to set for adding
*/
public void setResources(List<Resource> resources) {
this.resources = resources;
}
/**
* @param resource
* the resource to add for adding
*/
public void addResource(Resource resource) {
this.resources.add(resource);
}
@Override
public void validate() {
DBC.PRE.assertNotNull("Resources may not be null!", this.resources);
}
@Override
public void doCommand() {
this.resources.forEach(resource -> tx().lock(resource));
ResourceMap resourceMap = tx().getResourceMap();
this.resources.forEach(resource -> {
if (resourceMap.hasElement(tx(), resource.getType(), resource.getId())) {
String msg = MessageFormat.format("The Resource {0} already exists!", resource.getLocator());
throw new StrolchException(msg);
}
resourceMap.add(tx(), resource);
this.added.add(resource);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.added.isEmpty()) {
this.added.forEach(resource -> {
ResourceMap resourceMap = tx().getResourceMap();
if (resourceMap.hasElement(tx(), resource.getType(), resource.getId()))
resourceMap.remove(tx(), resource);
});
}
}
}

View File

@ -0,0 +1,91 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.activity.Activity;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class RemoveActivitiesCommand extends Command {
private List<Activity> activities = new ArrayList<>();
private final List<Activity> removed = new ArrayList<>();
public RemoveActivitiesCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param activities
* the activities to set for removal
*/
public void setActivities(List<Activity> activities) {
this.activities = activities;
}
/**
* @param activity
* the activity to add for removal
*/
public void addActivity(Activity activity) {
this.activities.add(activity);
}
@Override
public void validate() {
DBC.PRE.assertNotEmpty("Activities may not be empty!", this.activities);
}
@Override
public void doCommand() {
this.activities.forEach(activity -> tx().lock(activity));
ActivityMap activityMap = tx().getActivityMap();
this.activities.forEach(activity -> {
if (!activityMap.hasElement(tx(), activity.getType(), activity.getId())) {
String msg = "The Activity {0} can not be removed as it does not exist!!";
msg = MessageFormat.format(msg, activity.getLocator());
throw new StrolchException(msg);
}
activityMap.remove(tx(), activity);
this.removed.add(activity);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.removed.isEmpty()) {
this.activities.forEach(activity -> {
if (tx().isVersioningEnabled())
tx().getActivityMap().undoVersion(tx(), activity);
else
tx().getActivityMap().add(tx(), activity);
});
}
}
}

View File

@ -0,0 +1,91 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class RemoveOrdersCommand extends Command {
private List<Order> orders = new ArrayList<>();
private final List<Order> removed = new ArrayList<>();
public RemoveOrdersCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param orders
* the orders to set for removal
*/
public void setOrders(List<Order> orders) {
this.orders = orders;
}
/**
* @param order
* the order to add for removal
*/
public void addOrder(Order order) {
this.orders.add(order);
}
@Override
public void validate() {
DBC.PRE.assertNotEmpty("Orders may not be empty!", this.orders);
}
@Override
public void doCommand() {
this.orders.forEach(order -> tx().lock(order));
OrderMap orderMap = tx().getOrderMap();
this.orders.forEach(order -> {
if (!orderMap.hasElement(tx(), order.getType(), order.getId())) {
String msg = "The Order {0} can not be removed as it does not exist!!";
msg = MessageFormat.format(msg, order.getLocator());
throw new StrolchException(msg);
}
orderMap.remove(tx(), order);
this.removed.add(order);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.removed.isEmpty()) {
this.orders.forEach(order -> {
if (tx().isVersioningEnabled())
tx().getOrderMap().undoVersion(tx(), order);
else
tx().getOrderMap().add(tx(), order);
});
}
}
}

View File

@ -0,0 +1,91 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Resource;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class RemoveResourcesCommand extends Command {
private List<Resource> resources = new ArrayList<>();
private final List<Resource> removed = new ArrayList<>();
public RemoveResourcesCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param resources
* the resources to set for removal
*/
public void setResources(List<Resource> resources) {
this.resources = resources;
}
/**
* @param resource
* the resource to add for removal
*/
public void addResource(Resource resource) {
this.resources.add(resource);
}
@Override
public void validate() {
DBC.PRE.assertNotEmpty("Resources may not be empty!", this.resources);
}
@Override
public void doCommand() {
this.resources.forEach(resource -> tx().lock(resource));
ResourceMap resourceMap = tx().getResourceMap();
this.resources.forEach(resource -> {
if (!resourceMap.hasElement(tx(), resource.getType(), resource.getId())) {
String msg = "The Resource {0} can not be removed as it does not exist!!";
msg = MessageFormat.format(msg, resource.getLocator());
throw new StrolchException(msg);
}
resourceMap.remove(tx(), resource);
this.removed.add(resource);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.removed.isEmpty()) {
this.removed.forEach(resource -> {
if (tx().isVersioningEnabled())
tx().getResourceMap().undoVersion(tx(), resource);
else
tx().getResourceMap().add(tx(), resource);
});
}
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.activity.Activity;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class UpdateActivitiesCommand extends Command {
private List<Activity> activities = new ArrayList<>();
private final List<Activity> replaced = new ArrayList<>();
public UpdateActivitiesCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param activities
* the activities to set for updating
*/
public void setActivities(List<Activity> activities) {
this.activities = activities;
}
/**
* @param activity
* the activity to add for updating
*/
public void addActivity(Activity activity) {
this.activities.add(activity);
}
@Override
public void validate() {
DBC.PRE.assertNotNull("Activities may not be null!", this.activities);
}
@Override
public void doCommand() {
this.activities.forEach(activity -> tx().lock(activity));
ActivityMap activityMap = tx().getActivityMap();
this.activities.forEach(activity -> {
Activity replaced = activityMap.getBy(tx(), activity.getType(), activity.getId());
if (this.replaced == null) {
String msg = "The Activity {0} can not be updated as it does not exist!!";
msg = MessageFormat.format(msg, activity.getLocator());
throw new StrolchException(msg);
}
activityMap.update(tx(), activity);
this.replaced.add(replaced);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.replaced.isEmpty()) {
this.replaced.forEach(activity -> {
if (tx().isVersioningEnabled())
tx().getActivityMap().undoVersion(tx(), activity);
else
tx().getActivityMap().update(tx(), activity);
});
}
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class UpdateOrdersCommand extends Command {
private List<Order> orders = new ArrayList<>();
private final List<Order> replaced = new ArrayList<>();
public UpdateOrdersCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param orders
* the orders to set for updating
*/
public void setOrders(List<Order> orders) {
this.orders = orders;
}
/**
* @param resource
* the resource to add for updating
*/
public void addOrder(Order resource) {
this.orders.add(resource);
}
@Override
public void validate() {
DBC.PRE.assertNotNull("Orders may not be null!", this.orders);
}
@Override
public void doCommand() {
this.orders.forEach(resource -> tx().lock(resource));
OrderMap resourceMap = tx().getOrderMap();
this.orders.forEach(resource -> {
Order replaced = resourceMap.getBy(tx(), resource.getType(), resource.getId());
if (this.replaced == null) {
String msg = "The Order {0} can not be updated as it does not exist!!";
msg = MessageFormat.format(msg, resource.getLocator());
throw new StrolchException(msg);
}
resourceMap.update(tx(), resource);
this.replaced.add(replaced);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.replaced.isEmpty()) {
this.replaced.forEach(resource -> {
if (tx().isVersioningEnabled())
tx().getOrderMap().undoVersion(tx(), resource);
else
tx().getOrderMap().update(tx(), resource);
});
}
}
}

View File

@ -0,0 +1,92 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package li.strolch.persistence.api;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Resource;
import li.strolch.service.api.Command;
import li.strolch.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class UpdateResourcesCommand extends Command {
private List<Resource> resources = new ArrayList<>();
private final List<Resource> replaced = new ArrayList<>();
public UpdateResourcesCommand(StrolchTransaction tx) {
super(tx);
}
/**
* @param resources
* the resources to set for updating
*/
public void setResources(List<Resource> resources) {
this.resources = resources;
}
/**
* @param resource
* the resource to add for updating
*/
public void addResource(Resource resource) {
this.resources.add(resource);
}
@Override
public void validate() {
DBC.PRE.assertNotNull("Resources may not be null!", this.resources);
}
@Override
public void doCommand() {
this.resources.forEach(resource -> tx().lock(resource));
ResourceMap resourceMap = tx().getResourceMap();
this.resources.forEach(resource -> {
Resource replaced = resourceMap.getBy(tx(), resource.getType(), resource.getId());
if (this.replaced == null) {
String msg = "The Resource {0} can not be updated as it does not exist!!";
msg = MessageFormat.format(msg, resource.getLocator());
throw new StrolchException(msg);
}
resourceMap.update(tx(), resource);
this.replaced.add(replaced);
});
}
@Override
public void undo() {
if (tx().isRollingBack() && !this.replaced.isEmpty()) {
this.replaced.forEach(resource -> {
if (tx().isVersioningEnabled())
tx().getResourceMap().undoVersion(tx(), resource);
else
tx().getResourceMap().update(tx(), resource);
});
}
}
}