1 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 ; 48 import java.util.ArrayList ; 49 import java.util.Date ; 50 import java.util.Iterator ; 51 import java.util.List ; 52 53 import org.apache.commons.logging.Log; 54 55 60 public class CmsResourceComparison { 61 62 63 public static final String TYPE_ADDED = "added"; 64 65 66 public static final String TYPE_CHANGED = "changed"; 67 68 69 public static final String TYPE_REMOVED = "removed"; 70 71 72 public static final String TYPE_UNCHANGED = "unchanged"; 73 74 75 private static final Log LOG = CmsLog.getLog(CmsResourceComparison.class); 76 77 81 protected CmsResourceComparison() { 82 83 super(); 84 } 85 86 96 public static List compareAttributes(CmsObject cms, CmsFile file1, CmsFile file2) { 97 98 List comparedAttributes = new ArrayList (); 99 comparedAttributes.add(new CmsAttributeComparison( 100 Messages.GUI_HISTORY_COLS_SIZE_0, 101 String.valueOf(file1.getLength()), 102 String.valueOf(file2.getLength()))); 103 String release1; 104 if (CmsResource.DATE_RELEASED_DEFAULT == file1.getDateReleased()) { 105 release1 = "-"; 106 } else { 107 release1 = CmsDateUtil.getDateTime( 108 new Date (file1.getDateReleased()), 109 DateFormat.SHORT, 110 cms.getRequestContext().getLocale()); 111 } 112 String release2; 113 if (CmsResource.DATE_RELEASED_DEFAULT == file2.getDateReleased()) { 114 release2 = "-"; 115 } else { 116 release2 = CmsDateUtil.getDateTime( 117 new Date (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 expire1; 123 if (CmsResource.DATE_EXPIRED_DEFAULT == file1.getDateExpired()) { 124 expire1 = "-"; 125 } else { 126 expire1 = CmsDateUtil.getDateTime( 127 new Date (file1.getDateExpired()), 128 DateFormat.SHORT, 129 cms.getRequestContext().getLocale()); 130 } 131 String expire2; 132 if (CmsResource.DATE_EXPIRED_DEFAULT == file2.getDateExpired()) { 133 expire2 = "-"; 134 } else { 135 expire2 = CmsDateUtil.getDateTime( 136 new Date (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 dateLastModified1 = CmsDateUtil.getDateTime( 146 new Date (file1.getDateLastModified()), 147 DateFormat.SHORT, 148 cms.getRequestContext().getLocale()); 149 String dateLastModified2 = CmsDateUtil.getDateTime( 150 new Date (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 type1 = OpenCms.getResourceManager().getResourceType(file1.getTypeId()).getTypeName(); 159 String 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 dateCreated1 = CmsDateUtil.getDateTime( 165 new Date (file1.getDateCreated()), 166 DateFormat.SHORT, 167 cms.getRequestContext().getLocale()); 168 String dateCreated2 = CmsDateUtil.getDateTime( 169 new Date (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 userLastModified1 = CmsHistoryList.readUserNameOfBackupFile(cms, file1); 178 String 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 path1 = cms.getRequestContext().removeSiteRoot(file1.getRootPath()); 187 String 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 202 public static List compareProperties(CmsObject cms, CmsFile file1, CmsFile file2) throws CmsException { 203 204 List comparedProperties = new ArrayList (); 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 (); 218 List removedProperties = new ArrayList (properties1); 219 removedProperties.removeAll(properties2); 220 List addedProperties = new ArrayList (properties2); 221 addedProperties.removeAll(properties1); 222 List retainedProperties = new ArrayList (properties2); 223 retainedProperties.retainAll(properties1); 224 CmsProperty prop; 225 Iterator 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 value1 = ((CmsProperty)properties1.get(properties1.indexOf(prop))).getValue(); 247 String 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 |