/*
This library is part of octools
Copyright (c) 2000-2007 Valtech A/S (http://www.valtech.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
Alkacon OpenCms and the OpenCms logo are registered trademarks of
Alkacon Software GmbH in Germany, the USA and other countries
For further information about Alkacon Software GmbH, please see the
company website: http://www.alkacon.com
For further information about OpenCms, please see the
project website: http://www.opencms.org
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package dk.valtech.octools.file;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.opencms.db.CmsPublishedResource;
import org.opencms.file.CmsObject;
import org.opencms.main.CmsEvent;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.I_CmsEventListener;
import org.opencms.main.OpenCms;
import org.opencms.report.I_CmsReport;
import org.opencms.util.CmsUUID;
public class PublishProjectEventListener implements I_CmsEventListener {
private static final Log LOG = CmsLog.getLog(PublishProjectEventListener.class);
private I_PublishProjectEventHandler handler;
public PublishProjectEventListener(I_PublishProjectEventHandler handler) {
super();
this.handler = handler;
}
public PublishProjectEventListener() {
super();
}
public void cmsEvent(CmsEvent event) {
if (event.getType() == I_CmsEventListener.EVENT_PUBLISH_PROJECT) {
I_CmsReport report = null;
// CmsDbContext dbc;
try {
String exportUser = OpenCms.getDefaultUsers().getUserExport();
CmsObject cms = OpenCms.initCmsObject(exportUser);
CmsUUID projectId = (CmsUUID) event.getData().get(I_CmsEventListener.KEY_PROJECTID);
CmsUUID publishHistoryId = new CmsUUID((String)event.getData().get(I_CmsEventListener.KEY_PUBLISHID));
report = (I_CmsReport)event.getData().get(I_CmsEventListener.KEY_REPORT);
PublishProjectEventContext context = new PublishProjectEventContext(cms, projectId, report);
boolean isInitialized = false;
@SuppressWarnings("unchecked")
List<CmsPublishedResource> resources = cms.readPublishedResources(publishHistoryId);
Iterator<CmsPublishedResource> iter = resources.iterator();
while (iter.hasNext()) {
CmsPublishedResource resource = (CmsPublishedResource) iter.next();
if (handler.handlesResource(context, resource)) {
if (!isInitialized) {
handler.initialize(context);
isInitialized = true;
}
handler.handleResource(context, resource);
}
}
if (isInitialized) {
handler.finish(context);
}
} catch (CmsException e) {
if (report != null) {
report.addError(e);
report.println(e);
}
LOG.error("Error", e);
}
}
}
}