001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.lar;
016    
017    import com.liferay.portal.kernel.lar.ExportImportPathUtil;
018    import com.liferay.portal.kernel.lar.PortletDataContext;
019    import com.liferay.portal.kernel.lar.PortletDataHandlerKeys;
020    import com.liferay.portal.kernel.lar.StagedModelDataHandlerUtil;
021    import com.liferay.portal.kernel.lar.StagedModelType;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.util.MapUtil;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.xml.Element;
027    import com.liferay.portal.kernel.xml.ElementHandler;
028    import com.liferay.portal.kernel.xml.ElementProcessor;
029    
030    import java.io.StringReader;
031    
032    import java.util.Set;
033    
034    import org.apache.xerces.parsers.SAXParser;
035    
036    import org.xml.sax.InputSource;
037    
038    /**
039     * @author Zsolt Berentey
040     */
041    public class DeletionSystemEventImporter {
042    
043            public void importDeletionSystemEvents(
044                            final PortletDataContext portletDataContext)
045                    throws Exception {
046    
047                    if (!MapUtil.getBoolean(
048                                    portletDataContext.getParameterMap(),
049                                    PortletDataHandlerKeys.DELETIONS)) {
050    
051                            return;
052                    }
053    
054                    String xml = portletDataContext.getZipEntryAsString(
055                            ExportImportPathUtil.getSourceRootPath(portletDataContext) +
056                                    "/deletion-system-events.xml");
057    
058                    if (xml == null) {
059                            return;
060                    }
061    
062                    SAXParser saxParser = new SAXParser();
063    
064                    ElementHandler elementHandler = new ElementHandler(
065                            new ElementProcessor() {
066    
067                                    @Override
068                                    public void processElement(Element element) {
069                                            doImportDeletionSystemEvents(portletDataContext, element);
070                                    }
071    
072                            },
073                            new String[] {"deletion-system-event"});
074    
075                    saxParser.setContentHandler(elementHandler);
076    
077                    saxParser.parse(new InputSource(new StringReader(xml)));
078            }
079    
080            protected void doImportDeletionSystemEvents(
081                    PortletDataContext portletDataContext, Element element) {
082    
083                    Set<StagedModelType> stagedModelTypes =
084                            portletDataContext.getDeletionSystemEventStagedModelTypes();
085    
086                    StagedModelType stagedModelType = new StagedModelType(
087                            element.attributeValue("class-name"),
088                            element.attributeValue("referrer-class-name"));
089    
090                    if (!stagedModelTypes.contains(stagedModelType)) {
091                            return;
092                    }
093    
094                    try {
095                            StagedModelDataHandlerUtil.deleteStagedModel(
096                                    portletDataContext, element);
097                    }
098                    catch (Exception e) {
099                            if (_log.isWarnEnabled()) {
100                                    StringBundler sb = new StringBundler(4);
101    
102                                    sb.append("Unable to process deletion for ");
103                                    sb.append(stagedModelType);
104                                    sb.append(" with UUID ");
105                                    sb.append(element.attributeValue("uuid"));
106    
107                                    _log.warn(sb.toString());
108                            }
109                    }
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(
113                    DeletionSystemEventImporter.class);
114    
115    }