+/*******************************************************************************\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