001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.workspaces.repository.jcr; 017 018import java.util.HashMap; 019import java.util.HashSet; 020import java.util.Map; 021import java.util.Set; 022 023import javax.jcr.RepositoryException; 024 025import org.apache.avalon.framework.activity.Initializable; 026import org.apache.avalon.framework.component.Component; 027import org.apache.avalon.framework.logger.AbstractLogEnabled; 028 029/** 030 * Node state tracker. 031 */ 032public class NodeStateTracker extends AbstractLogEnabled implements Component, Initializable 033{ 034 035 /** The avalon role. */ 036 public static final String ROLE = NodeStateTracker.class.getName(); 037 038 /** The nodes created but not already saved. */ 039 protected Map<String, Set<String>> _newNodes; 040 041 @Override 042 public void initialize() throws Exception 043 { 044 _newNodes = new HashMap<>(); 045 } 046 047 /** 048 * Get the new nodes in a workspace. 049 * @param workspaceName the workspace name. 050 * @return the newly created nodes. 051 */ 052 public Set<String> getNewNodes(String workspaceName) 053 { 054 Set<String> nodes = new HashSet<>(); 055 056 if (_newNodes.containsKey(workspaceName)) 057 { 058 nodes.addAll(_newNodes.get(workspaceName)); 059 } 060 061 return nodes; 062 } 063 064 /** 065 * A node was added. 066 * @param workspaceName the workspace name. 067 * @param path the added node path. 068 * @throws RepositoryException if an error occurred 069 */ 070 public void nodeAdded(String workspaceName, String path) throws RepositoryException 071 { 072 Set<String> newNodes = null; 073 if (_newNodes.containsKey(workspaceName)) 074 { 075 newNodes = _newNodes.get(workspaceName); 076 } 077 else 078 { 079 newNodes = new HashSet<>(); 080 _newNodes.put(workspaceName, newNodes); 081 } 082 083 newNodes.add(path); 084 } 085 086 /** 087 * A node was removed. 088 * @param workspaceName the workspace name. 089 * @param path the removed node path. 090 * @throws RepositoryException if an error occurred 091 */ 092 public void nodeRemoved(String workspaceName, String path) throws RepositoryException 093 { 094 Set<String> newNodes = null; 095 if (_newNodes.containsKey(workspaceName)) 096 { 097 newNodes = _newNodes.get(workspaceName); 098 } 099 else 100 { 101 newNodes = new HashSet<>(); 102 _newNodes.put(workspaceName, newNodes); 103 } 104 105 newNodes.remove(path); 106 } 107 108 /** 109 * Clear the node set for a workspace. 110 * @param workspaceName the workspace name. 111 */ 112 public void clear(String workspaceName) 113 { 114 if (_newNodes.containsKey(workspaceName)) 115 { 116 _newNodes.get(workspaceName).clear(); 117 } 118 } 119 120}