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.convert;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.util.MaintenanceUtil;
020    import com.liferay.portlet.wiki.model.WikiPage;
021    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
022    import com.liferay.portlet.wiki.translators.ClassicToCreoleTranslator;
023    
024    import java.util.List;
025    
026    /**
027     * @author Jorge Ferrer
028     */
029    public class ConvertWikiCreole extends ConvertProcess {
030    
031            @Override
032            public String getDescription() {
033                    return "convert-wiki-pages-from-classic-wiki-to-creole-format";
034            }
035    
036            @Override
037            public boolean isEnabled() {
038                    boolean enabled = false;
039    
040                    try {
041                            int pagesCount = WikiPageLocalServiceUtil.getPagesCount(
042                                    "classic_wiki");
043    
044                            if (pagesCount > 0) {
045                                    enabled = true;
046                            }
047                    }
048                    catch (Exception e) {
049                            _log.error(e, e);
050                    }
051    
052                    return enabled;
053            }
054    
055            @Override
056            protected void doConvert() throws Exception {
057                    List<WikiPage> pages = WikiPageLocalServiceUtil.getPages(
058                            "classic_wiki");
059    
060                    ClassicToCreoleTranslator translator = new ClassicToCreoleTranslator();
061    
062                    MaintenanceUtil.appendStatus(
063                            "Converting " + pages.size() +
064                                    " Wiki pages from Classic Wiki to Creole format");
065    
066                    for (int i = 0; i < pages.size(); i++) {
067                            if ((i > 0) && (i % (pages.size() / 4) == 0)) {
068                                    MaintenanceUtil.appendStatus((i * 100. / pages.size()) + "%");
069                            }
070    
071                            WikiPage page = pages.get(i);
072    
073                            page.setFormat("creole");
074    
075                            page.setContent(translator.translate(page.getContent()));
076    
077                            WikiPageLocalServiceUtil.updateWikiPage(page);
078                    }
079            }
080    
081            private static Log _log = LogFactoryUtil.getLog(ConvertWikiCreole.class);
082    
083    }