From bff8658c2264d250bb60c23186a9548e8bde3867 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 16 Jul 2021 14:25:23 +0200 Subject: [PATCH] [New] Added bulk change commands for all objects --- .../persistence/api/AddActivitiesCommand.java | 89 ++++++++++++++++++ .../persistence/api/AddOrdersCommand.java | 89 ++++++++++++++++++ .../persistence/api/AddResourcesCommand.java | 89 ++++++++++++++++++ .../api/RemoveActivitiesCommand.java | 91 ++++++++++++++++++ .../persistence/api/RemoveOrdersCommand.java | 91 ++++++++++++++++++ .../api/RemoveResourcesCommand.java | 91 ++++++++++++++++++ .../api/UpdateActivitiesCommand.java | 92 +++++++++++++++++++ .../persistence/api/UpdateOrdersCommand.java | 92 +++++++++++++++++++ .../api/UpdateResourcesCommand.java | 92 +++++++++++++++++++ 9 files changed, 816 insertions(+) create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/AddActivitiesCommand.java create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/AddOrdersCommand.java create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/AddResourcesCommand.java create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveActivitiesCommand.java create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveOrdersCommand.java create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveResourcesCommand.java create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateActivitiesCommand.java create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateOrdersCommand.java create mode 100644 li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateResourcesCommand.java diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddActivitiesCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddActivitiesCommand.java new file mode 100644 index 000000000..fddcdd542 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddActivitiesCommand.java @@ -0,0 +1,89 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class AddActivitiesCommand extends Command { + + private List activities = new ArrayList<>(); + private final List added = new ArrayList<>(); + + public AddActivitiesCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param activities + * the activities to set for adding + */ + public void setActivities(List 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); + }); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddOrdersCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddOrdersCommand.java new file mode 100644 index 000000000..83d7e91b8 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddOrdersCommand.java @@ -0,0 +1,89 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class AddOrdersCommand extends Command { + + private List orders = new ArrayList<>(); + private final List added = new ArrayList<>(); + + public AddOrdersCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param orders + * the orders to set for adding + */ + public void setOrders(List 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); + }); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddResourcesCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddResourcesCommand.java new file mode 100644 index 000000000..7026219fc --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/AddResourcesCommand.java @@ -0,0 +1,89 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class AddResourcesCommand extends Command { + + private List resources = new ArrayList<>(); + private final List added = new ArrayList<>(); + + public AddResourcesCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param resources + * the resources to set for adding + */ + public void setResources(List 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); + }); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveActivitiesCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveActivitiesCommand.java new file mode 100644 index 000000000..922bb2544 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveActivitiesCommand.java @@ -0,0 +1,91 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class RemoveActivitiesCommand extends Command { + + private List activities = new ArrayList<>(); + private final List removed = new ArrayList<>(); + + public RemoveActivitiesCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param activities + * the activities to set for removal + */ + public void setActivities(List 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); + }); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveOrdersCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveOrdersCommand.java new file mode 100644 index 000000000..263563da0 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveOrdersCommand.java @@ -0,0 +1,91 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class RemoveOrdersCommand extends Command { + + private List orders = new ArrayList<>(); + private final List removed = new ArrayList<>(); + + public RemoveOrdersCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param orders + * the orders to set for removal + */ + public void setOrders(List 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); + }); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveResourcesCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveResourcesCommand.java new file mode 100644 index 000000000..d1a76510f --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/RemoveResourcesCommand.java @@ -0,0 +1,91 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class RemoveResourcesCommand extends Command { + + private List resources = new ArrayList<>(); + private final List removed = new ArrayList<>(); + + public RemoveResourcesCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param resources + * the resources to set for removal + */ + public void setResources(List 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); + }); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateActivitiesCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateActivitiesCommand.java new file mode 100644 index 000000000..d6b6b970a --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateActivitiesCommand.java @@ -0,0 +1,92 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class UpdateActivitiesCommand extends Command { + + private List activities = new ArrayList<>(); + private final List replaced = new ArrayList<>(); + + public UpdateActivitiesCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param activities + * the activities to set for updating + */ + public void setActivities(List 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); + }); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateOrdersCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateOrdersCommand.java new file mode 100644 index 000000000..f031065ab --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateOrdersCommand.java @@ -0,0 +1,92 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class UpdateOrdersCommand extends Command { + + private List orders = new ArrayList<>(); + private final List replaced = new ArrayList<>(); + + public UpdateOrdersCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param orders + * the orders to set for updating + */ + public void setOrders(List 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); + }); + } + } +} diff --git a/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateResourcesCommand.java b/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateResourcesCommand.java new file mode 100644 index 000000000..fc3e94bb1 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/persistence/api/UpdateResourcesCommand.java @@ -0,0 +1,92 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 + */ +public class UpdateResourcesCommand extends Command { + + private List resources = new ArrayList<>(); + private final List replaced = new ArrayList<>(); + + public UpdateResourcesCommand(StrolchTransaction tx) { + super(tx); + } + + /** + * @param resources + * the resources to set for updating + */ + public void setResources(List 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); + }); + } + } +}