001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ade;
029
030import org.opencms.db.CmsModificationContext;
031import org.opencms.file.CmsObject;
032import org.opencms.file.types.I_CmsResourceType;
033import org.opencms.jsp.CmsJspActionElement;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.relations.CmsCategoryService;
037import org.opencms.util.CmsStringUtil;
038
039import java.io.InputStream;
040import java.nio.charset.StandardCharsets;
041import java.util.ArrayList;
042
043import javax.servlet.http.HttpServletRequest;
044import javax.servlet.http.HttpServletResponse;
045import javax.servlet.jsp.PageContext;
046
047import org.apache.commons.logging.Log;
048
049public class CmsContentTest extends CmsJspActionElement {
050
051    private static final String BASE_FOLDER = "/shared/online/test";
052    private static final Log LOG = CmsLog.getLog(CmsContentTest.class);
053
054    public CmsContentTest(PageContext pageContext, HttpServletRequest request, HttpServletResponse response) {
055
056        super(pageContext, request, response);
057    }
058
059    public void run(String name) throws Exception {
060
061        int count = 500;
062        long start = System.currentTimeMillis();
063        try {
064
065            CmsObject cms = getCmsObject();
066            cms.lockResourceTemporary(BASE_FOLDER);
067            String folder = CmsStringUtil.joinPaths(BASE_FOLDER, name);
068            try {
069
070                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType("m-section");
071                String contentTemplate = "";
072                try (InputStream stream = getClass().getResourceAsStream("example.xml")) {
073                    contentTemplate = new String(contentTemplate.getBytes(), StandardCharsets.UTF_8);
074                }
075                final String finalTemplate = contentTemplate;
076                cms.createResource(folder, OpenCms.getResourceManager().getResourceType("folder"));
077                for (int i = 0; i < count; i++) {
078                    final int finalI = i;
079                    CmsModificationContext.doWithModificationContext(cms.getRequestContext(), () -> {
080                        byte[] content = finalTemplate.replace("NUMBER", "" + finalI).getBytes(StandardCharsets.UTF_8);
081                        String path = CmsStringUtil.joinPaths(folder, "content_" + finalI + ".xml");
082                        cms.createResource(path, type, content, new ArrayList<>());
083                        CmsCategoryService.getInstance().addResourceToCategory(cms, path, "foo");
084                        CmsCategoryService.getInstance().addResourceToCategory(cms, path, "bar");
085                        return null;
086                    });
087                }
088                OpenCms.getPublishManager().publishProject(cms);
089                OpenCms.getPublishManager().waitWhileRunning();
090            } finally {
091                cms.unlockResource(BASE_FOLDER);
092            }
093
094        } catch (Exception e) {
095            LOG.error(e.getLocalizedMessage(), e);
096        } finally {
097            long end = System.currentTimeMillis();
098            System.out.println("DURATION: " + (end - start));
099        }
100
101    }
102
103}