001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ui.dialogs.history.diff; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsProject; 032import org.opencms.file.CmsResource; 033import org.opencms.file.CmsResourceFilter; 034import org.opencms.gwt.shared.CmsHistoryResourceBean; 035import org.opencms.gwt.shared.CmsHistoryVersion; 036import org.opencms.main.CmsException; 037import org.opencms.main.OpenCms; 038import org.opencms.ui.A_CmsUI; 039import org.opencms.ui.util.table.CmsBeanTableBuilder; 040import org.opencms.workplace.comparison.CmsAttributeComparison; 041 042import java.util.List; 043 044import com.google.common.base.Objects; 045import com.google.common.base.Optional; 046import com.google.common.base.Predicate; 047import com.google.common.collect.Collections2; 048import com.google.common.collect.Lists; 049import com.vaadin.ui.Component; 050import com.vaadin.ui.Panel; 051import com.vaadin.v7.ui.Table; 052import com.vaadin.v7.ui.VerticalLayout; 053 054/** 055 * Abstract super class for displaying differences between a set of properties / attributes.<p> 056 */ 057public abstract class A_CmsAttributeDiff implements I_CmsDiffProvider { 058 059 /** 060 * Predicate used to check if an attribute comparison bean actually represents a difference.<p> 061 */ 062 public static class IsAttributeDifference implements Predicate<CmsAttributeComparison> { 063 064 /** 065 * @see com.google.common.base.Predicate#apply(java.lang.Object) 066 */ 067 public boolean apply(CmsAttributeComparison input) { 068 069 return !Objects.equal(input.getVersion1(), input.getVersion2()); 070 } 071 } 072 073 /** CSS class to mark the compare table. */ 074 private static final String COMPARE_TABLE_MARKER = "cms-compare-table-marker"; 075 076 /** 077 * Only selects the attributes comparisons that actually represent differences.<p> 078 * 079 * @param comps the attribute comparisons 080 * 081 * @return the list of attribute comparisons which correspond to actual differences 082 */ 083 public static List<CmsAttributeComparison> filterDifferent(List<CmsAttributeComparison> comps) { 084 085 return Lists.newArrayList(Collections2.filter(comps, new IsAttributeDifference())); 086 } 087 088 /** 089 * Builds a version string which can be used by CmsResourceComparison from a history resource bean.<p> 090 * 091 * This is not really a user-readable string, it is meant for internal usage. 092 * 093 * @param bean the history resource bean 094 * @return the version string 095 */ 096 public static String getVersionString(CmsHistoryResourceBean bean) { 097 098 if (bean.getVersion().getVersionNumber() != null) { 099 return bean.getVersion().getVersionNumber().toString(); 100 } else if (bean.getVersion().isOffline()) { 101 return "0"; 102 } else { 103 return "-1"; 104 } 105 } 106 107 /** 108 * Reads a historical resource for a history resource bean.<p> 109 * 110 * @param cms the CMS context 111 * @param bean the history resource bean 112 * @return the historical resource 113 * 114 * @throws CmsException if something goes wrong 115 */ 116 public static CmsResource readResource(CmsObject cms, CmsHistoryResourceBean bean) throws CmsException { 117 118 CmsHistoryVersion versionBean = bean.getVersion(); 119 if (versionBean.getVersionNumber() != null) { 120 return (CmsResource)(cms.readResource(bean.getStructureId(), versionBean.getVersionNumber().intValue())); 121 } else { 122 if (versionBean.isOnline()) { 123 CmsObject onlineCms = OpenCms.initCmsObject(cms); 124 onlineCms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID)); 125 return onlineCms.readResource(bean.getStructureId(), CmsResourceFilter.IGNORE_EXPIRATION); 126 } else { 127 return cms.readResource(bean.getStructureId(), CmsResourceFilter.IGNORE_EXPIRATION); 128 } 129 } 130 } 131 132 /** 133 * @see org.opencms.ui.dialogs.history.diff.I_CmsDiffProvider#diff(org.opencms.file.CmsObject, org.opencms.gwt.shared.CmsHistoryResourceBean, org.opencms.gwt.shared.CmsHistoryResourceBean) 134 */ 135 public Optional<Component> diff(CmsObject cms, CmsHistoryResourceBean v1, CmsHistoryResourceBean v2) 136 throws CmsException { 137 138 List<CmsAttributeComparison> attrCompare = getDifferences(cms, v1, v2); 139 if (attrCompare.isEmpty()) { 140 return Optional.absent(); 141 } 142 List<CmsPropertyCompareBean> compareBeans = Lists.newArrayList(); 143 144 for (CmsAttributeComparison comp : attrCompare) { 145 compareBeans.add(new CmsPropertyCompareBean(comp)); 146 } 147 CmsBeanTableBuilder<CmsPropertyCompareBean> builder = CmsBeanTableBuilder.newInstance( 148 CmsPropertyCompareBean.class, 149 A_CmsUI.get().getDisplayType().toString()); 150 builder.setMacroResolver(new CmsVersionMacroResolver(v1, v2)); 151 Table table = builder.buildTable(compareBeans); 152 table.setSortEnabled(false); 153 table.setWidth("100%"); 154 table.setPageLength(Math.min(12, compareBeans.size())); 155 table.setStyleName(COMPARE_TABLE_MARKER); 156 VerticalLayout vl = new VerticalLayout(); 157 vl.setMargin(true); 158 vl.addComponent(table); 159 Panel panel = new Panel(getCaption()); 160 panel.setContent(vl); 161 return Optional.fromNullable((Component)panel); 162 } 163 164 /** 165 * Gets the caption.<p> 166 * 167 * @return the caption 168 */ 169 public abstract String getCaption(); 170 171 /** 172 * Gets the attribute comparison beans representing the differences.<p> 173 * 174 * @param cms the CMS context 175 * @param v1 history bean representing the first version 176 * @param v2 history bean representing the second version 177 * @return the list of attribute comparisons for the given versions 178 * 179 * @throws CmsException if something goes wrong 180 */ 181 public abstract List<CmsAttributeComparison> getDifferences( 182 CmsObject cms, 183 CmsHistoryResourceBean v1, 184 CmsHistoryResourceBean v2) throws CmsException; 185 186}