-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.layer0.util;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.TreeSet;\r
-\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.common.CommentMetadata;\r
-import org.simantics.db.common.request.UniqueRead;\r
-import org.simantics.db.common.request.WriteRequest;\r
-import org.simantics.db.common.request.WriteResultRequest;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.adapter.Remover;\r
-import org.simantics.db.layer0.adapter.impl.AbstractRemover;\r
-import org.simantics.db.layer0.adapter.impl.EntityRemover;\r
-import org.simantics.db.layer0.exception.CannotRemoveException;\r
-import org.simantics.db.layer0.internal.SimanticsInternal;\r
-import org.simantics.utils.strings.AlphanumComparator;\r
-import org.simantics.utils.strings.EString;\r
-\r
-/**\r
- * Utility for working with {@link Remover}s.\r
- * \r
- * @author Tuukka Lehtonen\r
- * \r
- * @see Remover\r
- * @see AbstractRemover\r
- */\r
-public final class RemoverUtil {\r
-\r
- public static boolean canRemove(ReadGraph graph, Resource resource) throws DatabaseException {\r
- Remover remover = RemoverUtil.getPossibleRemover(graph, resource);\r
- if (remover != null) {\r
- String problem = remover.canRemove(graph, new HashMap<Object, Object>(4));\r
- if (problem != null) return false;\r
- }\r
- return true;\r
- }\r
- \r
- public static void remove(WriteGraph graph, Resource resource) throws DatabaseException {\r
- if (!tryRemover(graph, resource))\r
- EntityRemover.remove(graph, resource, true);\r
- }\r
-\r
- public static boolean tryRemover(WriteGraph graph, Resource resource) throws DatabaseException {\r
- Remover remover = getPossibleRemover(graph, resource);\r
- if (remover != null) {\r
- remover.remove(graph);\r
- return true;\r
- }\r
- return false;\r
- }\r
-\r
- /**\r
- * @param graph\r
- * @param resource\r
- * @return\r
- * @throws DatabaseException\r
- * @since 1.6\r
- */\r
- public static Remover getPossibleRemover(ReadGraph graph, Resource resource) throws DatabaseException {\r
- return graph.getPossibleAdapter(resource, Remover.class);\r
- }\r
-\r
- public static String testRemoval(final Collection<Resource> rs) throws DatabaseException {\r
- return SimanticsInternal.getSession().syncRequest(new UniqueRead<String>() {\r
- @Override\r
- public String perform(ReadGraph graph) throws DatabaseException {\r
- return testRemoval(graph, rs, null);\r
- }\r
- });\r
- }\r
-\r
- public static String testRemoval(ReadGraph graph, final Collection<Resource> rs, List<Remover> removers) throws DatabaseException {\r
- if (removers == null)\r
- removers = new ArrayList<Remover>(rs.size());\r
-\r
- for (Resource r : rs) {\r
- Remover remover = graph.getPossibleAdapter(r, Remover.class);\r
- if (remover != null)\r
- removers.add(remover);\r
- }\r
-\r
- Map<Object, Object> aux = new HashMap<Object, Object>();\r
- List<String> errors = new ArrayList<String>(2);\r
- for (Remover remover : removers) {\r
- String error = remover.canRemove(graph, aux);\r
- if (error != null)\r
- errors.add(error);\r
- }\r
- if (!errors.isEmpty()) {\r
- return EString.implode(errors);\r
- }\r
-\r
- return null;\r
- }\r
-\r
- public static String testRemoval(ReadGraph graph, final Collection<Resource> rs) throws DatabaseException {\r
- return testRemoval(graph, rs, null);\r
- }\r
-\r
- public static String checkedRemoval(final Collection<Resource> rs) throws DatabaseException {\r
- String problems = testRemoval(rs);\r
- if (problems != null) return problems; \r
-\r
- SimanticsInternal.getSession().syncRequest(new WriteRequest() {\r
-\r
- @Override\r
- public void perform(WriteGraph graph) throws DatabaseException {\r
- CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
- for (Resource r : rs) {\r
- // Add comment to change set.\r
- graph.addMetadata(cm.add("Removing " + r + "."));\r
- }\r
-\r
- List<Remover> removers = new ArrayList<Remover>(rs.size());\r
- String error = testRemoval(graph, rs, removers);\r
- if (error != null)\r
- throw new CannotRemoveException(error);\r
-\r
- for (Remover remover : removers)\r
- remover.remove(graph);\r
- }\r
-\r
- });\r
-\r
- return null;\r
- }\r
-\r
- /**\r
- * Try to remove the provided collection of resources. Removal will commence\r
- * only if all resources in the provided collection have the exact same\r
- * principal types.\r
- * \r
- * @param rs\r
- * collection of resources to remove in one go\r
- * @return <code>true</code> if removal was successful or <code>false</code>\r
- * if the principal types of all resources in the provided\r
- * collection did not match\r
- * @throws CannotRemoveException\r
- * if removal cannot commence because at least one of the\r
- * {@link Remover} instances adapted from the provided resource\r
- * set succeeds.\r
- * @throws DatabaseException\r
- */\r
- public static boolean tryCollectionRemover(final Collection<Resource> rs) throws DatabaseException {\r
- return SimanticsInternal.getSession().syncRequest(new WriteResultRequest<Boolean>() {\r
- @Override\r
- public Boolean perform(WriteGraph graph) throws DatabaseException {\r
- graph.markUndoPoint();\r
- // 1. make sure that all resources are of the same type\r
- Collection<Resource> principalTypes = null;\r
- for (Resource r : rs) {\r
- Collection<Resource> pts = graph.getPrincipalTypes(r);\r
- if (principalTypes == null) {\r
- principalTypes = pts;\r
- } else if (!principalTypes.equals(pts)) {\r
- //return false;\r
- StringBuilder sb = new StringBuilder();\r
- sb.append("Removing resources of different types at the same time is currently not supported.\n\nThe selection contained resources of the following types:\n");\r
- Set<Resource> differentTypes = new HashSet<Resource>();\r
- Set<String> typeNames = new TreeSet<String>(AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);\r
- for (Resource t : rs)\r
- differentTypes.addAll(graph.getPrincipalTypes(t));\r
- for (Resource t : differentTypes)\r
- typeNames.add(NameUtils.getSafeName(graph, t));\r
- for (String typeName : typeNames)\r
- sb.append("\t").append(typeName).append("\n");\r
- throw new CannotRemoveException(sb.toString());\r
- }\r
- }\r
-\r
- List<Remover> removers = new ArrayList<Remover>();\r
- Map<Remover, String> removedResources = new HashMap<Remover, String>();\r
- for (Resource r : rs) {\r
- Remover remover = graph.getPossibleAdapter(r, Remover.class);\r
- if (remover != null) {\r
- removers.add(remover);\r
- removedResources.put(remover, NameUtils.getSafeName(graph, r, true));\r
- }\r
- }\r
-\r
- Map<Object, Object> aux = new HashMap<Object, Object>();\r
- List<String> errors = new ArrayList<String>(removers.size());\r
- for (Remover remover : removers) {\r
- String error = remover.canRemove(graph, aux);\r
- if (error != null)\r
- errors.add(error);\r
- }\r
- if (!errors.isEmpty()) {\r
- throw new CannotRemoveException(EString.implode(errors));\r
- }\r
-\r
- CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
- for (Remover remover : removers) {\r
- remover.remove(graph);\r
- graph.addMetadata(cm.add("Removed " + removedResources.get(remover) + "."));\r
- }\r
-\r
- return true;\r
- }\r
- });\r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.layer0.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.common.CommentMetadata;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.request.WriteResultRequest;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.adapter.Remover;
+import org.simantics.db.layer0.adapter.impl.AbstractRemover;
+import org.simantics.db.layer0.adapter.impl.EntityRemover;
+import org.simantics.db.layer0.exception.CannotRemoveException;
+import org.simantics.db.layer0.internal.SimanticsInternal;
+import org.simantics.utils.strings.AlphanumComparator;
+import org.simantics.utils.strings.EString;
+
+/**
+ * Utility for working with {@link Remover}s.
+ *
+ * @author Tuukka Lehtonen
+ *
+ * @see Remover
+ * @see AbstractRemover
+ */
+public final class RemoverUtil {
+
+ public static boolean canRemove(ReadGraph graph, Resource resource) throws DatabaseException {
+ Remover remover = RemoverUtil.getPossibleRemover(graph, resource);
+ if (remover != null) {
+ String problem = remover.canRemove(graph, new HashMap<Object, Object>(4));
+ if (problem != null) return false;
+ }
+ return true;
+ }
+
+ public static void remove(WriteGraph graph, Resource resource) throws DatabaseException {
+ if (!tryRemover(graph, resource))
+ EntityRemover.remove(graph, resource, true);
+ }
+
+ public static boolean tryRemover(WriteGraph graph, Resource resource) throws DatabaseException {
+ Remover remover = getPossibleRemover(graph, resource);
+ if (remover != null) {
+ remover.remove(graph);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * @param graph
+ * @param resource
+ * @return
+ * @throws DatabaseException
+ * @since 1.6
+ */
+ public static Remover getPossibleRemover(ReadGraph graph, Resource resource) throws DatabaseException {
+ return graph.getPossibleAdapter(resource, Remover.class);
+ }
+
+ public static String testRemoval(final Collection<Resource> rs) throws DatabaseException {
+ return SimanticsInternal.getSession().syncRequest(new UniqueRead<String>() {
+ @Override
+ public String perform(ReadGraph graph) throws DatabaseException {
+ return testRemoval(graph, rs, null);
+ }
+ });
+ }
+
+ public static String testRemoval(ReadGraph graph, final Collection<Resource> rs, List<Remover> removers) throws DatabaseException {
+ if (removers == null)
+ removers = new ArrayList<Remover>(rs.size());
+
+ for (Resource r : rs) {
+ Remover remover = graph.getPossibleAdapter(r, Remover.class);
+ if (remover != null)
+ removers.add(remover);
+ }
+
+ Map<Object, Object> aux = new HashMap<Object, Object>();
+ List<String> errors = new ArrayList<String>(2);
+ for (Remover remover : removers) {
+ String error = remover.canRemove(graph, aux);
+ if (error != null)
+ errors.add(error);
+ }
+ if (!errors.isEmpty()) {
+ return EString.implode(errors);
+ }
+
+ return null;
+ }
+
+ public static String testRemoval(ReadGraph graph, final Collection<Resource> rs) throws DatabaseException {
+ return testRemoval(graph, rs, null);
+ }
+
+ public static String checkedRemoval(final Collection<Resource> rs) throws DatabaseException {
+ String problems = testRemoval(rs);
+ if (problems != null) return problems;
+
+ SimanticsInternal.getSession().syncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
+ for (Resource r : rs) {
+ // Add comment to change set.
+ graph.addMetadata(cm.add("Removing " + r + "."));
+ }
+
+ List<Remover> removers = new ArrayList<Remover>(rs.size());
+ String error = testRemoval(graph, rs, removers);
+ if (error != null)
+ throw new CannotRemoveException(error);
+
+ for (Remover remover : removers)
+ remover.remove(graph);
+ }
+
+ });
+
+ return null;
+ }
+
+ /**
+ * Try to remove the provided collection of resources. Removal will commence
+ * only if all resources in the provided collection have the exact same
+ * principal types.
+ *
+ * @param rs
+ * collection of resources to remove in one go
+ * @return <code>true</code> if removal was successful or <code>false</code>
+ * if the principal types of all resources in the provided
+ * collection did not match
+ * @throws CannotRemoveException
+ * if removal cannot commence because at least one of the
+ * {@link Remover} instances adapted from the provided resource
+ * set succeeds.
+ * @throws DatabaseException
+ */
+ public static boolean tryCollectionRemover(final Collection<Resource> rs) throws DatabaseException {
+ return SimanticsInternal.getSession().syncRequest(new WriteResultRequest<Boolean>() {
+ @Override
+ public Boolean perform(WriteGraph graph) throws DatabaseException {
+ graph.markUndoPoint();
+ // 1. make sure that all resources are of the same type
+ Collection<Resource> principalTypes = null;
+ for (Resource r : rs) {
+ Collection<Resource> pts = graph.getPrincipalTypes(r);
+ if (principalTypes == null) {
+ principalTypes = pts;
+ } else if (!principalTypes.equals(pts)) {
+ //return false;
+ StringBuilder sb = new StringBuilder();
+ sb.append("Removing resources of different types at the same time is currently not supported.\n\nThe selection contained resources of the following types:\n");
+ Set<Resource> differentTypes = new HashSet<Resource>();
+ Set<String> typeNames = new TreeSet<String>(AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);
+ for (Resource t : rs)
+ differentTypes.addAll(graph.getPrincipalTypes(t));
+ for (Resource t : differentTypes)
+ typeNames.add(NameUtils.getSafeName(graph, t));
+ for (String typeName : typeNames)
+ sb.append("\t").append(typeName).append("\n");
+ throw new CannotRemoveException(sb.toString());
+ }
+ }
+
+ List<Remover> removers = new ArrayList<Remover>();
+ Map<Remover, String> removedResources = new HashMap<Remover, String>();
+ for (Resource r : rs) {
+ Remover remover = graph.getPossibleAdapter(r, Remover.class);
+ if (remover != null) {
+ removers.add(remover);
+ removedResources.put(remover, NameUtils.getSafeName(graph, r, true));
+ }
+ }
+
+ Map<Object, Object> aux = new HashMap<Object, Object>();
+ List<String> errors = new ArrayList<String>(removers.size());
+ for (Remover remover : removers) {
+ String error = remover.canRemove(graph, aux);
+ if (error != null)
+ errors.add(error);
+ }
+ if (!errors.isEmpty()) {
+ throw new CannotRemoveException(EString.implode(errors));
+ }
+
+ CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
+ for (Remover remover : removers) {
+ remover.remove(graph);
+ graph.addMetadata(cm.add("Removed " + removedResources.get(remover) + "."));
+ }
+
+ return true;
+ }
+ });
+ }
+
+}