001/* 002 * Copyright 2018 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.cms.content.compare; 017 018import java.util.ArrayList; 019import java.util.List; 020import java.util.Optional; 021 022import org.ametys.cms.repository.Content; 023import org.ametys.plugins.repository.data.holder.ModelAwareDataHolder; 024import org.ametys.runtime.model.ViewItemAccessor; 025 026/** 027 * Result of a comparison between 2 data holders of a content. 028 */ 029public class ContentComparatorResult 030{ 031 private ModelAwareDataHolder _dataHolder1; 032 private ModelAwareDataHolder _dataHolder2; 033 private ViewItemAccessor _viewItemAccessor; 034 035 private boolean _areEquals; 036 private List<ContentComparatorChange> _changes; 037 038 /** 039 * Create a new result from 2 data holders 040 * @param dataHolder1 1st data holder 041 * @param dataHolder2 2nd data holder 042 */ 043 public ContentComparatorResult(ModelAwareDataHolder dataHolder1, ModelAwareDataHolder dataHolder2) 044 { 045 this(dataHolder1, dataHolder2, null); 046 } 047 048 /** 049 * Create a new result from 2 data holders and the view item accessor used for comparison 050 * @param dataHolder1 1st data holder 051 * @param dataHolder2 2nd data holder 052 * @param viewItemAccessor {@link ViewItemAccessor} 053 */ 054 public ContentComparatorResult(ModelAwareDataHolder dataHolder1, ModelAwareDataHolder dataHolder2, ViewItemAccessor viewItemAccessor) 055 { 056 this._dataHolder1 = dataHolder1; 057 this._dataHolder2 = dataHolder2; 058 this._viewItemAccessor = viewItemAccessor; 059 if (dataHolder1 != null && dataHolder2 != null) 060 { 061 this._areEquals = true; 062 } 063 else 064 { 065 this._areEquals = false; 066 } 067 this._changes = new ArrayList<>(); 068 } 069 070 /** 071 * Are the data holders equals ? 072 * @return true if equals 073 */ 074 public boolean areEquals() 075 { 076 return this._areEquals; 077 } 078 079 /** 080 * If data holders are not equals, a list of {@link ContentComparatorChange} is generated 081 * @return list of {@link ContentComparatorChange} 082 */ 083 public List<ContentComparatorChange> getChanges() 084 { 085 return this._changes; 086 } 087 088 /** 089 * Get the 1st data holder of the comparison 090 * @return 1st data holder 091 */ 092 public ModelAwareDataHolder getDataHolder1() 093 { 094 return this._dataHolder1; 095 } 096 097 /** 098 * Get the 2nd data holder of the comparison 099 * @return 2nd data holder 100 */ 101 public ModelAwareDataHolder getDataHolder2() 102 { 103 return this._dataHolder2; 104 } 105 106 /** 107 * Get the content of the 1st data holder of the comparison 108 * @return content of the 1st data holder 109 */ 110 public Content getContent1() 111 { 112 return Optional.ofNullable(_dataHolder1) 113 .map(ModelAwareDataHolder::getRootDataHolder) 114 .map(Content.class::cast) 115 .orElse(null); 116 } 117 118 /** 119 * Get the content of the 2nd data holder of the comparison 120 * @return content of the 2nd data holder 121 */ 122 public Content getContent2() 123 { 124 return Optional.ofNullable(_dataHolder2) 125 .map(ModelAwareDataHolder::getRootDataHolder) 126 .map(Content.class::cast) 127 .orElse(null); 128 } 129 130 /** 131 * get the {@link ViewItemAccessor} (generated from the view name, of from all attributes) 132 * @return {@link ViewItemAccessor} 133 */ 134 public ViewItemAccessor getViewItemAccessor() 135 { 136 return this._viewItemAccessor; 137 } 138 139 /** 140 * Add a change to the list 141 * @param change a new change 142 */ 143 public void addChange(ContentComparatorChange change) 144 { 145 this._changes.add(change); 146 setNotEquals(); 147 } 148 149 /** 150 * Declare that the 2 data holders are not equals 151 */ 152 public void setNotEquals() 153 { 154 this._areEquals = false; 155 } 156 157 @Override 158 public String toString() 159 { 160 return new StringBuilder("ContentComparatorResult [") 161 .append("dataHolder1=").append(_dataHolder1).append(", ") 162 .append("dataHolder2=").append(_dataHolder2).append(", ") 163 .append("viewItemAccessor=").append(_viewItemAccessor).append(", ") 164 .append("areEquals=").append(_areEquals).append(", ") 165 .append("changes=").append(_changes) 166 .append("]") 167 .toString(); 168 } 169}