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 GmbH & Co. KG, 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.workplace.commons; 029 030import org.opencms.main.CmsLog; 031import org.opencms.workplace.list.A_CmsListDialog; 032import org.opencms.workplace.list.CmsHtmlList; 033import org.opencms.workplace.list.CmsListExplorerColumn; 034import org.opencms.workplace.threads.A_CmsProgressThread; 035 036import java.util.Locale; 037 038import org.apache.commons.logging.Log; 039 040/** 041 * This class is used to build a list ({@link A_CmsListDialog}) in the background 042 * and to show the progress of building to the user.<p> 043 * 044 * To work correctly the operations while building the list have to update the 045 * actual progress.<p> 046 * 047 * @since 7.0.0 048 */ 049public class CmsProgressThread extends A_CmsProgressThread { 050 051 /** The log object for this class. */ 052 private static final Log LOG = CmsLog.getLog(CmsProgressThread.class); 053 054 /** The description to show for the progress. */ 055 private String m_description; 056 057 /** Stores the error occurred while the thread was running. */ 058 private Throwable m_error; 059 060 /** The time the thread has finished. */ 061 private long m_finishtime; 062 063 /** The key of this thread. */ 064 private String m_key; 065 066 /** The list the thread displays the progress of building it. */ 067 private A_CmsListDialog m_list; 068 069 /** The locale to use for this thread. */ 070 private Locale m_locale; 071 072 /** The actual progress of the thread. */ 073 private int m_progress; 074 075 /** The finished result as HTML of the list. */ 076 private String m_result; 077 078 /** The time the thread has started. */ 079 private long m_starttime; 080 081 /** 082 * Constructs a new progress thread with the given name.<p> 083 * 084 * @param list the list to use for the progress to display 085 * @param key the key of the thread 086 * @param locale the locale to use for this thread 087 */ 088 public CmsProgressThread(A_CmsListDialog list, String key, Locale locale) { 089 090 m_progress = 0; 091 m_list = list; 092 m_key = key; 093 m_starttime = 0; 094 m_finishtime = 0; 095 m_locale = locale; 096 097 setName(Messages.get().getBundle().key(Messages.GUI_PROGRESS_THREAD_NAME_1, key)); 098 } 099 100 /** 101 * Returns the description to show for the progress.<p> 102 * 103 * @return the description to show for the progress 104 */ 105 public String getDescription() { 106 107 return m_description; 108 } 109 110 /** 111 * Returns the error exception in case there was an error during the execution of 112 * this thread, null otherwise.<p> 113 * 114 * @return the error exception in case there was an error, null otherwise 115 */ 116 public Throwable getError() { 117 118 return m_error; 119 } 120 121 /** 122 * Returns the time the thread was finished.<p> 123 * 124 * @return the time the thread was finished 125 */ 126 public long getFinishTime() { 127 128 return m_finishtime; 129 } 130 131 /** 132 * Returns the key of this thread.<p> 133 * 134 * @return the key of this thread 135 */ 136 public String getKey() { 137 138 return m_key; 139 } 140 141 /** 142 * Returns the list of the progress bar.<p> 143 * 144 * @return the list of the progress bar 145 */ 146 public A_CmsListDialog getList() { 147 148 return m_list; 149 } 150 151 /** 152 * Returns the locale to use for this thread.<p> 153 * 154 * @return the locale to use for this thread 155 */ 156 public Locale getLocale() { 157 158 return m_locale; 159 } 160 161 /** 162 * Returns the progress.<p> 163 * 164 * @return the progress 165 */ 166 public int getProgress() { 167 168 return m_progress; 169 } 170 171 /** 172 * Returns the finished result as HTML of the list.<p> 173 * 174 * @return the finished result as HTML of the list 175 */ 176 public String getResult() { 177 178 return m_result; 179 } 180 181 /** 182 * Returns the time this report has been running.<p> 183 * 184 * @return the time this report has been running 185 */ 186 public synchronized long getRuntime() { 187 188 if (!isAlive()) { 189 return m_finishtime - m_starttime; 190 } else if (m_starttime > 0) { 191 return System.currentTimeMillis() - m_starttime; 192 } else { 193 return 0; 194 } 195 } 196 197 /** 198 * Returns the time the thread has started.<p> 199 * 200 * @return the time the thread has started 201 */ 202 public long getStartTime() { 203 204 return m_starttime; 205 } 206 207 /** 208 * @see java.lang.Thread#run() 209 */ 210 @Override 211 public void run() { 212 213 try { 214 if (LOG.isDebugEnabled()) { 215 LOG.debug(Messages.get().getBundle().key(Messages.LOG_PROGRESS_START_THREAD_1, getKey())); 216 } 217 m_starttime = System.currentTimeMillis(); 218 219 // calculate size of the list 220 m_list.refreshList(); 221 222 // create the list 223 CmsHtmlList list = m_list.getList(); 224 list.setBoxed(false); 225 226 StringBuffer result = new StringBuffer(); 227 228 result.append("<input type='hidden' name='result' value='"); 229 result.append(list.getTotalSize()).append("'>\n"); 230 result.append(CmsListExplorerColumn.getExplorerStyleDef()); 231 result.append("<div style='height:200px; overflow: auto;'>\n"); 232 result.append(list.listHtml()); 233 result.append("</div>\n"); 234 235 m_result = result.toString(); 236 } catch (Throwable t) { 237 m_error = t; 238 239 if (LOG.isErrorEnabled()) { 240 LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_EXECUTING_THREAD_0), t); 241 } 242 } finally { 243 m_finishtime = System.currentTimeMillis(); 244 if (LOG.isDebugEnabled()) { 245 LOG.debug(Messages.get().getBundle().key(Messages.LOG_PROGRESS_FINISHED_THREAD_1, getKey())); 246 } 247 } 248 249 } 250 251 /** 252 * Sets the description to show for the progress.<p> 253 * 254 * @param description the description to show for the progress to set 255 */ 256 public void setDescription(String description) { 257 258 m_description = description; 259 } 260 261 /** 262 * Sets the locale to use for this thread.<p> 263 * 264 * @param locale the locale to use for this thread to set 265 */ 266 public void setLocale(Locale locale) { 267 268 m_locale = locale; 269 } 270 271 /** 272 * Sets the progress.<p> 273 * 274 * @param progress the progress to set 275 */ 276 public void setProgress(int progress) { 277 278 m_progress = progress; 279 } 280 281}