001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public String getDescription() {
032                    return "convert-wiki-pages-from-classic-wiki-to-creole-format";
033            }
034    
035            public boolean isEnabled() {
036                    boolean enabled = false;
037    
038                    try {
039                            int pagesCount = WikiPageLocalServiceUtil.getPagesCount(
040                                    "classic_wiki");
041    
042                            if (pagesCount > 0) {
043                                    enabled = true;
044                            }
045                    }
046                    catch (Exception e) {
047                            _log.error(e, e);
048                    }
049    
050                    return enabled;
051            }
052    
053            protected void doConvert() throws Exception {
054                    List<WikiPage> pages = WikiPageLocalServiceUtil.getPages(
055                            "classic_wiki");
056    
057                    ClassicToCreoleTranslator translator = new ClassicToCreoleTranslator();
058    
059                    MaintenanceUtil.appendStatus(
060                            "Converting " + pages.size() +
061                                    " Wiki pages from Classic Wiki to Creole format");
062    
063                    for (int i = 0; i < pages.size(); i++) {
064                            if ((i > 0) && (i % (pages.size() / 4) == 0)) {
065                                    MaintenanceUtil.appendStatus((i * 100. / pages.size()) + "%");
066                            }
067    
068                            WikiPage page = pages.get(i);
069    
070                            page.setFormat("creole");
071    
072                            page.setContent(translator.translate(page.getContent()));
073    
074                            WikiPageLocalServiceUtil.updateWikiPage(page);
075                    }
076            }
077    
078            private static Log _log = LogFactoryUtil.getLog(ConvertWikiCreole.class);
079    
080    }