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.apps.dbmanager; 029 030import org.opencms.ui.A_CmsUI; 031import org.opencms.ui.CmsVaadinUtils; 032import org.opencms.ui.apps.CmsAppWorkplaceUi; 033import org.opencms.ui.apps.Messages; 034import org.opencms.ui.apps.search.CmsSearchReplaceSettings; 035import org.opencms.ui.apps.search.CmsSourceSearchApp; 036import org.opencms.ui.apps.search.CmsSourceSearchAppConfiguration; 037import org.opencms.ui.apps.search.CmsSourceSearchForm.SearchType; 038import org.opencms.util.CmsDateUtil; 039 040import java.util.ArrayList; 041import java.util.Collections; 042import java.util.Date; 043import java.util.Iterator; 044import java.util.List; 045 046import com.vaadin.ui.Alignment; 047import com.vaadin.ui.Button; 048import com.vaadin.ui.Button.ClickEvent; 049import com.vaadin.ui.Component; 050import com.vaadin.v7.shared.ui.label.ContentMode; 051import com.vaadin.v7.ui.HorizontalLayout; 052import com.vaadin.v7.ui.Label; 053import com.vaadin.v7.ui.VerticalLayout; 054 055/** 056 * Class for the database-statistic result list.<p> 057 */ 058public class CmsResourceTypeStatResultList { 059 060 /**Time after an entry gets removed.*/ 061 private static final long MAX_TIME = 24 * 60 * 60 * 1000; //24h 062 063 /**List of results.*/ 064 private List<CmsResourceTypeStatResult> m_results = new ArrayList<CmsResourceTypeStatResult>(); 065 066 /**Was an entry updated? -> remove old entry and add new one to the top of the list.*/ 067 private boolean m_updated; 068 069 /** 070 * Method to initialize the list. 071 * 072 * @param resList a given instance or null 073 * @return an instance 074 * */ 075 public static CmsResourceTypeStatResultList init(CmsResourceTypeStatResultList resList) { 076 077 if (resList == null) { 078 return new CmsResourceTypeStatResultList(); 079 } 080 081 resList.deleteOld(); 082 return resList; 083 } 084 085 /** 086 * Adds a result to the list.<p> 087 * 088 * @param result to be added 089 */ 090 public void addResult(CmsResourceTypeStatResult result) { 091 092 if (!m_results.contains(result)) { 093 m_results.add(result); 094 m_updated = false; 095 } else { 096 m_results.remove(result); 097 m_results.add(result); 098 m_updated = true; 099 } 100 } 101 102 /** 103 * Deletes entries which are older than MAX_TIME.<p> 104 */ 105 public void deleteOld() { 106 107 Iterator<CmsResourceTypeStatResult> iterator = m_results.iterator(); 108 while (iterator.hasNext()) { 109 CmsResourceTypeStatResult res = iterator.next(); 110 if (isToOld(res)) { 111 iterator.remove(); 112 } 113 } 114 } 115 116 /** 117 * Checks if result list is empty.<p> 118 * 119 * @return true if result list is empty 120 */ 121 public boolean isEmpty() { 122 123 return m_results.size() == 0; 124 } 125 126 /** 127 * Sets the layout.<p> 128 * 129 * @param layout to display the result in 130 * @param addAll indicates if the whole list should be added or just the last item 131 */ 132 public void setVerticalLayout(VerticalLayout layout, boolean addAll) { 133 134 if (m_results.size() > 0) { 135 if (addAll) { 136 for (CmsResourceTypeStatResult result : m_results) { 137 layout.addComponent(getLayoutFromResult(result), 0); 138 } 139 } else { 140 CmsResourceTypeStatResult statResult = m_results.get(m_results.size() - 1); 141 142 if (m_updated) { 143 removeRow(layout, statResult); 144 } 145 layout.addComponent(getLayoutFromResult(statResult), 0); 146 } 147 } 148 } 149 150 /** 151 * Creates a result row.<p> 152 * 153 * @param statResult result to be displayed 154 * @return a row with information about result 155 */ 156 private HorizontalLayout getLayoutFromResult(final CmsResourceTypeStatResult statResult) { 157 158 HorizontalLayout hLayout = new HorizontalLayout(); 159 hLayout.setWidth("100%"); 160 hLayout.setHeight("60px"); 161 hLayout.addStyleName("o-report"); 162 163 Label result = new Label(); 164 result.setContentMode(ContentMode.HTML); 165 result.addStyleName("v-scrollable"); 166 //result.addStyleName("o-report"); 167 result.setValue(statResult.getResult()); 168 169 Label type = new Label(); 170 type.setWidth("200px"); 171 type.setContentMode(ContentMode.HTML); 172 type.addStyleName("v-scrollable"); 173 //type.addStyleName("o-report"); 174 type.setValue(statResult.getTypeTitle()); 175 176 Label time = new Label(); 177 time.setWidth("180px"); 178 time.setContentMode(ContentMode.HTML); 179 time.addStyleName("v-scrollable"); 180 //time.addStyleName("o-report"); 181 time.setValue( 182 CmsDateUtil.getDateTime( 183 new Date(statResult.getTimestamp()), 184 java.text.DateFormat.DATE_FIELD, 185 A_CmsUI.get().getLocale())); 186 187 Button showList = new Button(CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_STATS_LIST_0)); 188 showList.setWidth("100px"); 189 190 showList.addClickListener(new Button.ClickListener() { 191 192 private static final long serialVersionUID = 2665235403970750534L; 193 194 public void buttonClick(ClickEvent event) { 195 196 CmsSearchReplaceSettings settings = new CmsSearchReplaceSettings(); 197 settings.setPaths(Collections.singletonList("/")); 198 settings.setSiteRoot(statResult.getSiteRoot()); 199 settings.setSearchpattern(".*"); 200 if (statResult.getType() != null) { 201 settings.setTypes(statResult.getType().getTypeName()); 202 } 203 settings.setType(SearchType.fullText); 204 CmsAppWorkplaceUi.get().showApp( 205 CmsSourceSearchAppConfiguration.APP_ID, 206 CmsSourceSearchApp.generateState(settings)); 207 } 208 }); 209 210 hLayout.addComponent(type); 211 hLayout.addComponent(result); 212 hLayout.addComponent(time); 213 hLayout.addComponent(showList); 214 hLayout.setExpandRatio(result, 1); 215 hLayout.setData(statResult); 216 hLayout.setSpacing(true); 217 hLayout.setComponentAlignment(showList, Alignment.MIDDLE_CENTER); 218 hLayout.setComponentAlignment(time, Alignment.MIDDLE_CENTER); 219 hLayout.setComponentAlignment(result, Alignment.MIDDLE_CENTER); 220 hLayout.setComponentAlignment(type, Alignment.MIDDLE_CENTER); 221 return hLayout; 222 } 223 224 /** 225 * Checks if given results is to old.<p> 226 * 227 * @param res to be checked 228 * @return true if result should be removed 229 */ 230 private boolean isToOld(CmsResourceTypeStatResult res) { 231 232 return ((System.currentTimeMillis() - res.getTimestamp()) > MAX_TIME); 233 } 234 235 /** 236 * Removes result row representing given results.<p> 237 * 238 * @param layout with results 239 * @param result to be removed 240 */ 241 private void removeRow(VerticalLayout layout, CmsResourceTypeStatResult result) { 242 243 Component componentToRemove = null; 244 Iterator<Component> iterator = layout.iterator(); 245 while (iterator.hasNext()) { 246 Component component = iterator.next(); 247 if (component instanceof HorizontalLayout) { 248 if (result.equals(((HorizontalLayout)component).getData())) { 249 componentToRemove = component; 250 } 251 } 252 } 253 if (componentToRemove != null) { 254 layout.removeComponent(componentToRemove); 255 } 256 } 257}