001    /**
002     * Copyright (c) 2000-present 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 static DeletionSystemEventImporter getInstance() {
044                    return _instance;
045            }
046    
047            public void importDeletionSystemEvents(
048                            final PortletDataContext portletDataContext)
049                    throws Exception {
050    
051                    if (!MapUtil.getBoolean(
052                                    portletDataContext.getParameterMap(),
053                                    PortletDataHandlerKeys.DELETIONS)) {
054    
055                            return;
056                    }
057    
058                    String xml = portletDataContext.getZipEntryAsString(
059                            ExportImportPathUtil.getSourceRootPath(portletDataContext) +
060                                    "/deletion-system-events.xml");
061    
062                    if (xml == null) {
063                            return;
064                    }
065    
066                    SAXParser saxParser = new SAXParser();
067    
068                    ElementHandler elementHandler = new ElementHandler(
069                            new ElementProcessor() {
070    
071                                    @Override
072                                    public void processElement(Element element) {
073                                            doImportDeletionSystemEvents(portletDataContext, element);
074                                    }
075    
076                            },
077                            new String[] {"deletion-system-event"});
078    
079                    saxParser.setContentHandler(elementHandler);
080    
081                    saxParser.parse(new InputSource(new StringReader(xml)));
082            }
083    
084            protected void doImportDeletionSystemEvents(
085                    PortletDataContext portletDataContext, Element element) {
086    
087                    Set<StagedModelType> stagedModelTypes =
088                            portletDataContext.getDeletionSystemEventStagedModelTypes();
089    
090                    StagedModelType stagedModelType = new StagedModelType(
091                            element.attributeValue("class-name"),
092                            element.attributeValue("referrer-class-name"));
093    
094                    if (!stagedModelTypes.contains(stagedModelType)) {
095                            return;
096                    }
097    
098                    try {
099                            StagedModelDataHandlerUtil.deleteStagedModel(
100                                    portletDataContext, element);
101                    }
102                    catch (Exception e) {
103                            if (_log.isWarnEnabled()) {
104                                    StringBundler sb = new StringBundler(4);
105    
106                                    sb.append("Unable to process deletion for ");
107                                    sb.append(stagedModelType);
108                                    sb.append(" with UUID ");
109                                    sb.append(element.attributeValue("uuid"));
110    
111                                    _log.warn(sb.toString());
112                            }
113                    }
114            }
115    
116            private DeletionSystemEventImporter() {
117            }
118    
119            private static final Log _log = LogFactoryUtil.getLog(
120                    DeletionSystemEventImporter.class);
121    
122            private static final DeletionSystemEventImporter _instance =
123                    new DeletionSystemEventImporter();
124    
125    }