KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > comparison > CmsResourceComparison


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/comparison/CmsResourceComparison.java,v $
3  * Date : $Date: 2006/03/27 14:52:44 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2005 Alkacon Software (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, 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.workplace.comparison;
33
34 import org.opencms.file.CmsBackupResource;
35 import org.opencms.file.CmsFile;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsProperty;
38 import org.opencms.file.CmsResource;
39 import org.opencms.loader.CmsLoaderException;
40 import org.opencms.main.CmsException;
41 import org.opencms.main.CmsLog;
42 import org.opencms.main.OpenCms;
43 import org.opencms.util.CmsDateUtil;
44 import org.opencms.workplace.commons.CmsHistoryList;
45 import org.opencms.workplace.commons.Messages;
46
47 import java.text.DateFormat JavaDoc;
48 import java.util.ArrayList JavaDoc;
49 import java.util.Date JavaDoc;
50 import java.util.Iterator JavaDoc;
51 import java.util.List JavaDoc;
52
53 import org.apache.commons.logging.Log;
54
55 /**
56  * Comparison of two OpenCms resources.<p>
57  *
58  * @author Jan Baudisch
59  */

60 public class CmsResourceComparison {
61
62     /** Constant indicating that an item (e.g. element or property) has been added.<p> */
63     public static final String JavaDoc TYPE_ADDED = "added";
64
65     /** Constant indicating that an item has been changed.<p> */
66     public static final String JavaDoc TYPE_CHANGED = "changed";
67
68     /** Constant indicating that an item has been removed.<p> */
69     public static final String JavaDoc TYPE_REMOVED = "removed";
70
71     /** Constant indicating that an item has not been changed.<p> */
72     public static final String JavaDoc TYPE_UNCHANGED = "unchanged";
73
74     /** The log object for this class. */
75     private static final Log LOG = CmsLog.getLog(CmsResourceComparison.class);
76
77     /**
78      * Constructs a new resource comparison object.<p>
79      *
80      */

81     protected CmsResourceComparison() {
82
83         super();
84     }
85
86     /**
87      * Helper method that collects all meta attributes of the two file versions and
88      * finds out, which of the attributes were added, removed, modified or remain unchanged.<p>
89      *
90      * @param cms the CmsObject to use
91      * @param file1 the first file to read the properties from
92      * @param file2 the second file to read the properties from
93      *
94      * @return a list of the compared attributes
95      */

96     public static List compareAttributes(CmsObject cms, CmsFile file1, CmsFile file2) {
97
98         List comparedAttributes = new ArrayList JavaDoc();
99         comparedAttributes.add(new CmsAttributeComparison(
100             Messages.GUI_HISTORY_COLS_SIZE_0,
101             String.valueOf(file1.getLength()),
102             String.valueOf(file2.getLength())));
103         String JavaDoc release1;
104         if (CmsResource.DATE_RELEASED_DEFAULT == file1.getDateReleased()) {
105             release1 = "-";
106         } else {
107             release1 = CmsDateUtil.getDateTime(
108                 new Date JavaDoc(file1.getDateReleased()),
109                 DateFormat.SHORT,
110                 cms.getRequestContext().getLocale());
111         }
112         String JavaDoc release2;
113         if (CmsResource.DATE_RELEASED_DEFAULT == file2.getDateReleased()) {
114             release2 = "-";
115         } else {
116             release2 = CmsDateUtil.getDateTime(
117                 new Date JavaDoc(file2.getDateReleased()),
118                 DateFormat.SHORT,
119                 cms.getRequestContext().getLocale());
120         }
121         comparedAttributes.add(new CmsAttributeComparison(Messages.GUI_LABEL_DATE_RELEASED_0, release1, release2));
122         String JavaDoc expire1;
123         if (CmsResource.DATE_EXPIRED_DEFAULT == file1.getDateExpired()) {
124             expire1 = "-";
125         } else {
126             expire1 = CmsDateUtil.getDateTime(
127                 new Date JavaDoc(file1.getDateExpired()),
128                 DateFormat.SHORT,
129                 cms.getRequestContext().getLocale());
130         }
131         String JavaDoc expire2;
132         if (CmsResource.DATE_EXPIRED_DEFAULT == file2.getDateExpired()) {
133             expire2 = "-";
134         } else {
135             expire2 = CmsDateUtil.getDateTime(
136                 new Date JavaDoc(file2.getDateExpired()),
137                 DateFormat.SHORT,
138                 cms.getRequestContext().getLocale());
139         }
140         comparedAttributes.add(new CmsAttributeComparison(Messages.GUI_LABEL_DATE_EXPIRED_0, expire1, expire2));
141         comparedAttributes.add(new CmsAttributeComparison(
142             Messages.GUI_PERMISSION_INTERNAL_0,
143             String.valueOf((file1.getFlags() & CmsResource.FLAG_INTERNAL) > 0),
144             String.valueOf((file2.getFlags() & CmsResource.FLAG_INTERNAL) > 0)));
145         String JavaDoc dateLastModified1 = CmsDateUtil.getDateTime(
146             new Date JavaDoc(file1.getDateLastModified()),
147             DateFormat.SHORT,
148             cms.getRequestContext().getLocale());
149         String JavaDoc dateLastModified2 = CmsDateUtil.getDateTime(
150             new Date JavaDoc(file2.getDateLastModified()),
151             DateFormat.SHORT,
152             cms.getRequestContext().getLocale());
153         comparedAttributes.add(new CmsAttributeComparison(
154             Messages.GUI_LABEL_DATE_LAST_MODIFIED_0,
155             dateLastModified1,
156             dateLastModified2));
157         try {
158             String JavaDoc type1 = OpenCms.getResourceManager().getResourceType(file1.getTypeId()).getTypeName();
159             String JavaDoc type2 = OpenCms.getResourceManager().getResourceType(file2.getTypeId()).getTypeName();
160             comparedAttributes.add(new CmsAttributeComparison(Messages.GUI_HISTORY_COLS_FILE_TYPE_0, type1, type2));
161         } catch (CmsLoaderException e) {
162             LOG.debug(e.getMessage(), e);
163         }
164         String JavaDoc dateCreated1 = CmsDateUtil.getDateTime(
165             new Date JavaDoc(file1.getDateCreated()),
166             DateFormat.SHORT,
167             cms.getRequestContext().getLocale());
168         String JavaDoc dateCreated2 = CmsDateUtil.getDateTime(
169             new Date JavaDoc(file2.getDateCreated()),
170             DateFormat.SHORT,
171             cms.getRequestContext().getLocale());
172         comparedAttributes.add(new CmsAttributeComparison(
173             Messages.GUI_HISTORY_COLS_DATE_PUBLISHED_0,
174             dateCreated1,
175             dateCreated2));
176         try {
177             String JavaDoc userLastModified1 = CmsHistoryList.readUserNameOfBackupFile(cms, file1);
178             String JavaDoc userLastModified2 = CmsHistoryList.readUserNameOfBackupFile(cms, file2);
179             comparedAttributes.add(new CmsAttributeComparison(
180                 Messages.GUI_LABEL_USER_LAST_MODIFIED_0,
181                 userLastModified1,
182                 userLastModified2));
183         } catch (CmsException e) {
184             LOG.error(e.getMessage(), e);
185         }
186         String JavaDoc path1 = cms.getRequestContext().removeSiteRoot(file1.getRootPath());
187         String JavaDoc path2 = cms.getRequestContext().removeSiteRoot(file2.getRootPath());
188         comparedAttributes.add(new CmsAttributeComparison(Messages.GUI_HISTORY_COLS_RESOURCE_PATH_0, path1, path2));
189         return comparedAttributes;
190     }
191
192     /**
193      * Helper method that finds out, which of the properties were added, removed, modified or remain unchanged.<p>
194      *
195      * @param cms the CmsObject to use
196      * @param file1 the first file to read the properties from
197      * @param file2 the second file to read the properties from
198      *
199      * @return a list of the compared attributes
200      * @throws CmsException if something goes wrong
201      */

202     public static List compareProperties(CmsObject cms, CmsFile file1, CmsFile file2) throws CmsException {
203
204         List comparedProperties = new ArrayList JavaDoc();
205         List properties1;
206         if (file1 instanceof CmsBackupResource) {
207             properties1 = cms.readBackupPropertyObjects((CmsBackupResource)file1);
208         } else {
209             properties1 = cms.readPropertyObjects(file1, false);
210         }
211         List properties2;
212         if (file2 instanceof CmsBackupResource) {
213             properties2 = cms.readBackupPropertyObjects((CmsBackupResource)file2);
214         } else {
215             properties2 = cms.readPropertyObjects(file2, false);
216         }
217         comparedProperties = new ArrayList JavaDoc();
218         List removedProperties = new ArrayList JavaDoc(properties1);
219         removedProperties.removeAll(properties2);
220         List addedProperties = new ArrayList JavaDoc(properties2);
221         addedProperties.removeAll(properties1);
222         List retainedProperties = new ArrayList JavaDoc(properties2);
223         retainedProperties.retainAll(properties1);
224         CmsProperty prop;
225         Iterator JavaDoc i = addedProperties.iterator();
226         while (i.hasNext()) {
227             prop = (CmsProperty)i.next();
228             comparedProperties.add(new CmsAttributeComparison(
229                 prop.getName(),
230                 "",
231                 prop.getValue(),
232                 CmsResourceComparison.TYPE_ADDED));
233         }
234         i = removedProperties.iterator();
235         while (i.hasNext()) {
236             prop = (CmsProperty)i.next();
237             comparedProperties.add(new CmsAttributeComparison(
238                 prop.getName(),
239                 prop.getValue(),
240                 "",
241                 CmsResourceComparison.TYPE_REMOVED));
242         }
243         i = retainedProperties.iterator();
244         while (i.hasNext()) {
245             prop = (CmsProperty)i.next();
246             String JavaDoc value1 = ((CmsProperty)properties1.get(properties1.indexOf(prop))).getValue();
247             String JavaDoc value2 = ((CmsProperty)properties2.get(properties2.indexOf(prop))).getValue();
248             if (value1.equals(value2)) {
249                 comparedProperties.add(new CmsAttributeComparison(
250                     prop.getName(),
251                     value1,
252                     value2,
253                     CmsResourceComparison.TYPE_UNCHANGED));
254             } else {
255                 comparedProperties.add(new CmsAttributeComparison(
256                     prop.getName(),
257                     value1,
258                     value2,
259                     CmsResourceComparison.TYPE_CHANGED));
260             }
261         }
262         return comparedProperties;
263     }
264 }
265
Popular Tags