KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > test > OpenCmsTestResourceStorage


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/test/OpenCmsTestResourceStorage.java,v $
3  * Date : $Date: 2005/06/27 23:22:21 $
4  * Version: $Revision: 1.19 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.test;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.main.CmsException;
37
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * Storage object for storing all attributes of vfs resources.<p>
43  *
44  * @author Michael Emmerich
45  * @version $Revision: 1.19 $
46  */

47 public class OpenCmsTestResourceStorage {
48
49     /** the name of the default storage. */
50     public static String JavaDoc DEFAULT_STORAGE = "default";
51
52     /** the name of the default storage. */
53     public static String JavaDoc GLOBAL_STORAGE = "global";
54     
55     /** the name of the storage. */
56     private String JavaDoc m_name;
57     
58     /** storeage for precalculation of states. **/
59     private Map JavaDoc m_precalcState;
60
61     /** Strings for mapping the filename. */
62     private String JavaDoc m_sourceNameMapping;
63
64     /** internal storage. */
65     private Map JavaDoc m_storage;
66
67     /** Prefix mapping for target name. */
68     private String JavaDoc m_targetNameMapping;
69     
70     /**
71      * Creates a new OpenCmsTestResourceStorage.<p>
72      *
73      * @param name the name of the storage
74      */

75     public OpenCmsTestResourceStorage(String JavaDoc name) {
76
77         m_storage = new HashMap JavaDoc();
78         m_precalcState = new HashMap JavaDoc();
79         m_sourceNameMapping = null;
80         m_targetNameMapping = null;
81         m_name = name;
82     }
83
84     /**
85      * Adds a CmsResource to the resource storage.<p>
86      *
87      * @param cms the CmsObject
88      * @param resourceName the resource name to add
89      * @param resource the resource to add
90      * @throws CmsException if something goes wrong
91      */

92     public void add(CmsObject cms, String JavaDoc resourceName, CmsResource resource) throws CmsException {
93
94         m_storage.put(resourceName, new OpenCmsTestResourceStorageEntry(cms, resourceName, resource));
95         m_precalcState.put(resourceName, preCalculateState(resource));
96     }
97     
98     
99     /**
100      * Gets an entry from the storage.<p>
101      *
102      * @param resourceName the name of the resource to get
103      * @return OpenCmsTestResourceStorageEntry with all the attributes of a CmsResource
104      * @throws Exception in case something goes wrong
105      */

106     public OpenCmsTestResourceStorageEntry get(String JavaDoc resourceName) throws Exception JavaDoc {
107
108         String JavaDoc mappedResourceName = mapResourcename(resourceName);
109
110         OpenCmsTestResourceStorageEntry entry = null;
111         entry = (OpenCmsTestResourceStorageEntry)m_storage.get(mappedResourceName);
112
113         if (entry == null) {
114             throw new Exception JavaDoc("Not found in storage " + resourceName + " -> " + mappedResourceName);
115         }
116
117         return entry;
118     }
119     
120     /**
121      * Gets the name of the storage.<p>
122      *
123      * @return the name of the storage
124      */

125     public String JavaDoc getName() {
126         
127         return m_name;
128     }
129     
130     /**
131      * Returns the size of the storage.<p>
132      *
133      * @return the size of the storage
134      */

135     public int size() {
136         return m_storage.size();
137     }
138     
139     /**
140      * Gets an precalculate resource state from the storage.<p>
141      *
142      * @param resourceName the name of the resource to get the state
143      * @return precalculated resource state
144      * @throws Exception in case something goes wrong
145      */

146     public int getPreCalculatedState(String JavaDoc resourceName) throws Exception JavaDoc {
147         
148          String JavaDoc mappedResourceName = mapResourcename(resourceName);
149          
150          Integer JavaDoc state = null;
151          state = (Integer JavaDoc)m_precalcState.get(mappedResourceName);
152          if (state == null) {
153             throw new Exception JavaDoc("Not found in storage " + resourceName + " -> " + mappedResourceName);
154         }
155         return state.intValue();
156     }
157     
158     
159
160     /**
161      * Returns the source name mapping.<p>
162      *
163      * @return the source name mapping
164      */

165     public String JavaDoc getSourceNameMapping() {
166
167         return m_sourceNameMapping;
168     }
169
170     /**
171      * Returns the target name mapping.<p>
172      *
173      * @return the the target name mapping
174      */

175     public String JavaDoc getTargetNameMapping() {
176
177         return m_targetNameMapping;
178     }
179
180     /**
181      * Sets the mapping for resourcenames.<p>
182      *
183      * @param source the source resource name
184      * @param target the target resource name
185      */

186     public void setMapping(String JavaDoc source, String JavaDoc target) {
187
188         m_sourceNameMapping = source;
189         m_targetNameMapping = target;
190     }
191     
192     /**
193      * Resets the mapping for resourcenames.<p>
194      */

195     public void resetMapping() {
196
197         m_sourceNameMapping = null;
198         m_targetNameMapping = null;
199     }
200     
201     /**
202      * Does the name mapping of a resourceName.<p>
203      *
204      * This is required to find resources in the resource storage afer their path in the vfs
205      * has changed (e.g. after a copy operation).<p>
206      *
207      * @param resourceName the resource name to map
208      * @return mapped resource name
209      */

210     public String JavaDoc mapResourcename(String JavaDoc resourceName) {
211         
212         // only modify the name if we have set some kind of mapping
213
if (m_sourceNameMapping != null && m_targetNameMapping != null) {
214             // check if the resourcename starts with the source map name
215
if (resourceName.startsWith(m_sourceNameMapping)) {
216                 // exchange the prefix with the target map name
217
resourceName = m_targetNameMapping + resourceName.substring(m_sourceNameMapping.length());
218             }
219         }
220         return resourceName;
221     }
222
223     
224     /**
225      * Precalculates the state of a resource after an operation based on its state before
226      * the operation is excecuted.<p>
227      *
228      * The following states are precalculated:
229      * <ul>
230      * <li>Unchanged -> Changed</li>
231      * <li>Changed -> Changed</li>
232      * <li>New -> New</li>
233      * <li>Deleted -> Deleted</li>
234      * </ul>
235      * @param res the resource
236      * @return new precalculated state
237      */

238     private Integer JavaDoc preCalculateState(CmsResource res) {
239         
240         int newState = CmsResource.STATE_UNCHANGED;
241         int state = res.getState();
242         switch (state) {
243             case CmsResource.STATE_UNCHANGED:
244                 newState = CmsResource.STATE_CHANGED;
245                 break;
246             case CmsResource.STATE_CHANGED:
247                 newState = CmsResource.STATE_CHANGED;
248                 break;
249             case CmsResource.STATE_NEW:
250                 newState = CmsResource.STATE_NEW;
251                 break;
252             case CmsResource.STATE_DELETED:
253                 newState = CmsResource.STATE_DELETED;
254                 break;
255             default:
256                 newState = CmsResource.STATE_UNCHANGED;
257                 break;
258         }
259         return new Integer JavaDoc(newState);
260     }
261 }
Popular Tags