Search in sources :

Example 1 with I_CmsResourceType

use of org.opencms.file.types.I_CmsResourceType in project opencms-core by alkacon.

the class CmsCloneModuleThread method cloneResourceTypes.

/**
 * Clones/copies the resource types.<p>
 *
 * @param sourceModule the source module
 * @param targetModule the target module
 * @param sourcePathPart the source path part
 * @param targetPathPart the target path part
 * @param keys the map where to put in the messages of the resource type
 *
 * @return a map with source resource types as key and the taregt resource types as value
 */
private Map<I_CmsResourceType, I_CmsResourceType> cloneResourceTypes(CmsModule sourceModule, CmsModule targetModule, String sourcePathPart, String targetPathPart, Map<String, String> keys) {
    Map<I_CmsResourceType, I_CmsResourceType> resourceTypeMapping = new HashMap<I_CmsResourceType, I_CmsResourceType>();
    List<I_CmsResourceType> targetResourceTypes = new ArrayList<I_CmsResourceType>();
    for (I_CmsResourceType sourceResType : targetModule.getResourceTypes()) {
        // get the class name attribute
        String className = sourceResType.getClassName();
        // create the class instance
        I_CmsResourceType targetResType;
        try {
            if (className != null) {
                className = className.trim();
            }
            int newId = -1;
            boolean exists = true;
            do {
                newId = new Random().nextInt((99999)) + 10000;
                try {
                    OpenCms.getResourceManager().getResourceType(newId);
                } catch (CmsLoaderException e) {
                    exists = false;
                }
            } while (exists);
            targetResType = (I_CmsResourceType) Class.forName(className).newInstance();
            for (String mapping : sourceResType.getConfiguredMappings()) {
                targetResType.addMappingType(mapping);
            }
            targetResType.setAdjustLinksFolder(sourceResType.getAdjustLinksFolder());
            if (targetResType instanceof A_CmsResourceType) {
                A_CmsResourceType concreteTargetResType = (A_CmsResourceType) targetResType;
                for (CmsProperty prop : sourceResType.getConfiguredDefaultProperties()) {
                    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(prop.getValue())) {
                        prop.setStructureValue(prop.getStructureValue().replaceAll(sourceModule.getName(), targetModule.getName()).replaceAll(sourcePathPart, targetPathPart));
                        prop.setResourceValue(prop.getResourceValue().replaceAll(sourceModule.getName(), targetModule.getName()).replaceAll(sourcePathPart, targetPathPart));
                    }
                    concreteTargetResType.addDefaultProperty(prop);
                }
                for (CmsConfigurationCopyResource conres : sourceResType.getConfiguredCopyResources()) {
                    concreteTargetResType.addCopyResource(conres.getSource(), conres.getTarget(), conres.getTypeString());
                }
            }
            for (Map.Entry<String, String> entry : sourceResType.getConfiguration().entrySet()) {
                targetResType.addConfigurationParameter(entry.getKey(), entry.getValue().replaceAll(sourceModule.getName(), targetModule.getName()));
            }
            targetResType.setAdditionalModuleResourceType(true);
            targetResType.initConfiguration(alterPrefix(sourceResType.getTypeName(), m_cloneInfo.getSourceNamePrefix(), m_cloneInfo.getTargetNamePrefix()), newId + "", sourceResType.getClassName());
            keys.put(sourceResType.getTypeName(), targetResType.getTypeName());
            targetResourceTypes.add(targetResType);
            resourceTypeMapping.put(sourceResType, targetResType);
        } catch (Exception e) {
            // resource type is unknown, use dummy class to import the module resources
            targetResType = new CmsResourceTypeUnknown();
            // write an error to the log
            LOG.error(org.opencms.configuration.Messages.get().getBundle().key(org.opencms.configuration.Messages.ERR_UNKNOWN_RESTYPE_CLASS_2, className, targetResType.getClass().getName()), e);
        }
    }
    targetModule.setResourceTypes(targetResourceTypes);
    return resourceTypeMapping;
}
Also used : CmsLoaderException(org.opencms.loader.CmsLoaderException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CmsExportPoint(org.opencms.db.CmsExportPoint) CmsXmlException(org.opencms.xml.CmsXmlException) CmsException(org.opencms.main.CmsException) CmsVfsResourceNotFoundException(org.opencms.file.CmsVfsResourceNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CmsLoaderException(org.opencms.loader.CmsLoaderException) A_CmsResourceType(org.opencms.file.types.A_CmsResourceType) CmsResourceTypeUnknown(org.opencms.file.types.CmsResourceTypeUnknown) I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) Random(java.util.Random) CmsConfigurationCopyResource(org.opencms.configuration.CmsConfigurationCopyResource) CmsProperty(org.opencms.file.CmsProperty) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with I_CmsResourceType

use of org.opencms.file.types.I_CmsResourceType in project opencms-core by alkacon.

the class CmsCloneModuleThread method run.

/**
 * @see java.lang.Thread#run()
 */
@Override
public void run() {
    CmsModule sourceModule = OpenCms.getModuleManager().getModule(m_cloneInfo.getSourceModuleName());
    // clone the module object
    CmsModule targetModule = sourceModule.clone();
    targetModule.setName(m_cloneInfo.getName());
    targetModule.setNiceName(m_cloneInfo.getNiceName());
    targetModule.setDescription(m_cloneInfo.getDescription());
    targetModule.setAuthorEmail(m_cloneInfo.getAuthorEmail());
    targetModule.setAuthorName(m_cloneInfo.getAuthorName());
    targetModule.setGroup(m_cloneInfo.getGroup());
    targetModule.setActionClass(m_cloneInfo.getActionClass());
    CmsObject cms = getCms();
    CmsProject currentProject = cms.getRequestContext().getCurrentProject();
    try {
        CmsProject workProject = cms.createProject("Clone_module_work_project", "Clone modulee work project", OpenCms.getDefaultUsers().getGroupAdministrators(), OpenCms.getDefaultUsers().getGroupAdministrators(), CmsProject.PROJECT_TYPE_TEMPORARY);
        cms.getRequestContext().setCurrentProject(workProject);
        // store the module paths
        String sourceModulePath = CmsWorkplace.VFS_PATH_MODULES + sourceModule.getName() + "/";
        String targetModulePath = CmsWorkplace.VFS_PATH_MODULES + targetModule.getName() + "/";
        // store the package name as path part
        String sourcePathPart = sourceModule.getName().replaceAll("\\.", "/");
        String targetPathPart = targetModule.getName().replaceAll("\\.", "/");
        // store the classes folder paths
        String sourceClassesPath = targetModulePath + PATH_CLASSES + sourcePathPart + "/";
        String targetClassesPath = targetModulePath + PATH_CLASSES + targetPathPart + "/";
        // copy the resources
        cms.copyResource(sourceModulePath, targetModulePath);
        // check if we have to create the classes folder
        if (cms.existsResource(sourceClassesPath)) {
            // in the source module a classes folder was defined,
            // now create all sub-folders for the package structure in the new module folder
            createTargetClassesFolder(targetModule, sourceClassesPath, targetModulePath + PATH_CLASSES);
            // delete the origin classes folder
            deleteSourceClassesFolder(targetModulePath, sourcePathPart, targetPathPart);
        }
        // TODO: clone module dependencies
        // adjust the export points
        cloneExportPoints(sourceModule, targetModule, sourcePathPart, targetPathPart);
        // adjust the resource type names and IDs
        Map<String, String> descKeys = new HashMap<String, String>();
        Map<I_CmsResourceType, I_CmsResourceType> resTypeMap = cloneResourceTypes(sourceModule, targetModule, sourcePathPart, targetPathPart, descKeys);
        // adjust the explorer type names and store referred icons and message keys
        Map<String, String> iconPaths = new HashMap<String, String>();
        cloneExplorerTypes(targetModule, iconPaths, descKeys);
        // rename the icon file names
        cloneExplorerTypeIcons(iconPaths);
        // adjust the module resources
        adjustModuleResources(sourceModule, targetModule, sourcePathPart, targetPathPart, iconPaths);
        // search and replace the localization keys
        if (getCms().existsResource(targetClassesPath)) {
            List<CmsResource> props = cms.readResources(targetClassesPath, CmsResourceFilter.DEFAULT_FILES);
            replacesMessages(descKeys, props);
        }
        int type = OpenCms.getResourceManager().getResourceType(CmsVfsBundleManager.TYPE_XML_BUNDLE).getTypeId();
        CmsResourceFilter filter = CmsResourceFilter.requireType(type);
        List<CmsResource> resources = cms.readResources(targetModulePath, filter);
        replacesMessages(descKeys, resources);
        renameXmlVfsBundles(resources, targetModule, sourceModule.getName());
        List<CmsResource> allModuleResources = cms.readResources(targetModulePath, CmsResourceFilter.ALL);
        replacePath(sourceModulePath, targetModulePath, allModuleResources);
        // search and replace paths
        replaceModuleName();
        // replace formatter paths
        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_cloneInfo.getFormatterTargetModule()) && !targetModule.getResourceTypes().isEmpty()) {
            replaceFormatterPaths(targetModule);
        }
        adjustConfigs(targetModule, resTypeMap);
        // now unlock and publish the project
        getReport().println(Messages.get().container(Messages.RPT_PUBLISH_PROJECT_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
        cms.unlockProject(workProject.getUuid());
        OpenCms.getPublishManager().publishProject(cms, getReport());
        OpenCms.getPublishManager().waitWhileRunning();
        getReport().println(Messages.get().container(Messages.RPT_PUBLISH_PROJECT_END_0), I_CmsReport.FORMAT_HEADLINE);
        // add the imported module to the module manager
        OpenCms.getModuleManager().addModule(cms, targetModule);
        // reinitialize the resource manager with additional module resource types if necessary
        if (targetModule.getResourceTypes() != Collections.EMPTY_LIST) {
            OpenCms.getResourceManager().initialize(cms);
        }
        // reinitialize the workplace manager with additional module explorer types if necessary
        if (targetModule.getExplorerTypes() != Collections.EMPTY_LIST) {
            OpenCms.getWorkplaceManager().addExplorerTypeSettings(targetModule);
        }
        // re-initialize the workplace
        OpenCms.getWorkplaceManager().initialize(cms);
        // fire "clear caches" event to reload all cached resource bundles
        OpenCms.fireCmsEvent(I_CmsEventListener.EVENT_CLEAR_CACHES, new HashMap<String, Object>());
        // following changes will not be published right now, switch back to previous project
        cms.getRequestContext().setCurrentProject(currentProject);
        // change resource types and schema locations
        if (isTrue(m_cloneInfo.getChangeResourceTypes())) {
            changeResourceTypes(resTypeMap);
        }
        // adjust container pages
        CmsObject cloneCms = OpenCms.initCmsObject(cms);
        if (isTrue(m_cloneInfo.getApplyChangesEverywhere())) {
            cloneCms.getRequestContext().setSiteRoot("/");
        }
        if (m_cloneInfo.isRewriteContainerPages()) {
            CmsResourceFilter f = CmsResourceFilter.requireType(CmsResourceTypeXmlContainerPage.getContainerPageTypeId());
            List<CmsResource> allContainerPages = cloneCms.readResources("/", f);
            replacePath(sourceModulePath, targetModulePath, allContainerPages);
        }
    } catch (Throwable e) {
        LOG.error(e.getLocalizedMessage(), e);
        getReport().addError(e);
    } finally {
        cms.getRequestContext().setCurrentProject(currentProject);
    }
}
Also used : HashMap(java.util.HashMap) CmsExportPoint(org.opencms.db.CmsExportPoint) I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsModule(org.opencms.module.CmsModule) CmsObject(org.opencms.file.CmsObject) CmsResource(org.opencms.file.CmsResource) CmsResourceFilter(org.opencms.file.CmsResourceFilter) CmsObject(org.opencms.file.CmsObject) CmsProject(org.opencms.file.CmsProject)

Example 3 with I_CmsResourceType

use of org.opencms.file.types.I_CmsResourceType in project opencms-core by alkacon.

the class CmsCloneModuleThread method adjustConfigs.

/**
 * Adjusts the module configuration file and the formatter configurations.<p>
 *
 * @param targetModule the target module
 * @param resTypeMap the resource type mapping
 *
 * @throws CmsException if something goes wrong
 * @throws UnsupportedEncodingException if the file content could not be read with the determined encoding
 */
private void adjustConfigs(CmsModule targetModule, Map<I_CmsResourceType, I_CmsResourceType> resTypeMap) throws CmsException, UnsupportedEncodingException {
    String modPath = CmsWorkplace.VFS_PATH_MODULES + targetModule.getName() + "/";
    CmsObject cms = getCms();
    if (((m_cloneInfo.getSourceNamePrefix() != null) && (m_cloneInfo.getTargetNamePrefix() != null)) || !m_cloneInfo.getSourceNamePrefix().equals(m_cloneInfo.getTargetNamePrefix())) {
        // replace resource type names in formatter configurations
        List<CmsResource> resources = cms.readResources(modPath, CmsResourceFilter.requireType(OpenCms.getResourceManager().getResourceType(CmsFormatterConfigurationCache.TYPE_FORMATTER_CONFIG)));
        String source = "<Type><!\\[CDATA\\[" + m_cloneInfo.getSourceNamePrefix();
        String target = "<Type><!\\[CDATA\\[" + m_cloneInfo.getTargetNamePrefix();
        Function<String, String> replaceType = new ReplaceAll(source, target);
        for (CmsResource resource : resources) {
            transformResource(resource, replaceType);
        }
        resources.clear();
    }
    // replace resource type names in module configuration
    try {
        CmsResource config = cms.readResource(modPath + CmsADEManager.CONFIG_FILE_NAME, CmsResourceFilter.requireType(OpenCms.getResourceManager().getResourceType(CmsADEManager.MODULE_CONFIG_TYPE)));
        Function<String, String> substitution = Functions.identity();
        for (Map.Entry<I_CmsResourceType, I_CmsResourceType> mapping : resTypeMap.entrySet()) {
            substitution = Functions.compose(new ReplaceAll(mapping.getKey().getTypeName(), mapping.getValue().getTypeName()), substitution);
        }
        // Either replace prefix in or prepend it to the folder name value
        Function<String, String> replaceFolderName = new ReplaceAll("(<Folder>[ \n]*<Name><!\\[CDATA\\[)(" + m_cloneInfo.getSourceNamePrefix() + ")?", "$1" + m_cloneInfo.getTargetNamePrefix());
        substitution = Functions.compose(replaceFolderName, substitution);
        transformResource(config, substitution);
    } catch (CmsVfsResourceNotFoundException e) {
        LOG.info(e.getLocalizedMessage(), e);
    }
}
Also used : I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsVfsResourceNotFoundException(org.opencms.file.CmsVfsResourceNotFoundException) CmsObject(org.opencms.file.CmsObject) CmsResource(org.opencms.file.CmsResource) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with I_CmsResourceType

use of org.opencms.file.types.I_CmsResourceType in project opencms-core by alkacon.

the class CmsCloneModuleThread method replaceFormatterPaths.

/**
 * Replaces the referenced formatters within the new XSD files with the new formatter paths.<p>
 *
 * @param targetModule the target module
 *
 * @throws CmsException if something goes wrong
 * @throws UnsupportedEncodingException if the file content could not be read with the determined encoding
 */
private void replaceFormatterPaths(CmsModule targetModule) throws CmsException, UnsupportedEncodingException {
    CmsResource formatterSourceFolder = getCms().readResource("/system/modules/" + m_cloneInfo.getFormatterSourceModule() + "/");
    CmsResource formatterTargetFolder = getCms().readResource("/system/modules/" + m_cloneInfo.getFormatterTargetModule() + "/");
    for (I_CmsResourceType type : targetModule.getResourceTypes()) {
        String schemaPath = type.getConfiguration().get("schema");
        CmsResource res = getCms().readResource(schemaPath);
        CmsFile file = getCms().readFile(res);
        if (CmsResourceTypeXmlContent.isXmlContent(file)) {
            CmsXmlContent xmlContent = CmsXmlContentFactory.unmarshal(getCms(), file);
            xmlContent.setAutoCorrectionEnabled(true);
            file = xmlContent.correctXmlStructure(getCms());
        }
        String encoding = CmsLocaleManager.getResourceEncoding(getCms(), file);
        String content = new String(file.getContents(), encoding);
        content = content.replaceAll(formatterSourceFolder.getRootPath(), formatterTargetFolder.getRootPath());
        file.setContents(content.getBytes(encoding));
        getCms().writeFile(file);
    }
}
Also used : I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) CmsXmlContent(org.opencms.xml.content.CmsXmlContent) CmsFile(org.opencms.file.CmsFile) CmsResource(org.opencms.file.CmsResource)

Example 5 with I_CmsResourceType

use of org.opencms.file.types.I_CmsResourceType in project opencms-core by alkacon.

the class CmsResourceWrapperUtils method writePropertyFile.

/**
 * Takes the content which should be formatted as a property file and set them
 * as properties to the resource.<p>
 *
 * @see #createPropertyFile(CmsObject, CmsResource, String)
 *
 * @param cms the initialized CmsObject
 * @param resourcename the name of the resource where to set the properties
 * @param content the properties to set (formatted as a property file)
 *
 * @throws CmsException if something goes wrong
 */
public static void writePropertyFile(CmsObject cms, String resourcename, byte[] content) throws CmsException {
    Properties properties = new Properties();
    try {
        String props = CmsEncoder.createString(content, CmsEncoder.ENCODING_UTF_8);
        props = unescapeString(props);
        props = CmsEncoder.encodeJavaEntities(props, CmsEncoder.ENCODING_ISO_8859_1);
        byte[] modContent = props.getBytes(CmsEncoder.ENCODING_ISO_8859_1);
        properties.load(new ByteArrayInputStream(modContent));
        List<CmsProperty> propList = new ArrayList<CmsProperty>();
        Iterator<Map.Entry<Object, Object>> it = properties.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Object, Object> e = it.next();
            String key = (String) e.getKey();
            String value = (String) e.getValue();
            if (key.endsWith(SUFFIX_PROP_SHARED)) {
                propList.add(new CmsProperty(key.substring(0, key.length() - SUFFIX_PROP_SHARED.length()), null, value));
            } else if (key.endsWith(SUFFIX_PROP_INDIVIDUAL)) {
                propList.add(new CmsProperty(key.substring(0, key.length() - SUFFIX_PROP_INDIVIDUAL.length()), value, null));
            }
        }
        cms.writePropertyObjects(resourcename, propList);
        String newType = properties.getProperty(PROPERTY_RESOURCE_TYPE);
        if (newType != null) {
            newType = newType.trim();
            if (OpenCms.getResourceManager().hasResourceType(newType)) {
                I_CmsResourceType newTypeObj = OpenCms.getResourceManager().getResourceType(newType);
                cms.chtype(resourcename, newTypeObj.getTypeId());
            }
        }
    } catch (IOException e) {
    // noop
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) Properties(java.util.Properties) I_CmsResourceType(org.opencms.file.types.I_CmsResourceType) ByteArrayInputStream(java.io.ByteArrayInputStream) CmsProperty(org.opencms.file.CmsProperty) CmsObject(org.opencms.file.CmsObject) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

I_CmsResourceType (org.opencms.file.types.I_CmsResourceType)152 CmsResource (org.opencms.file.CmsResource)80 CmsException (org.opencms.main.CmsException)68 ArrayList (java.util.ArrayList)45 CmsObject (org.opencms.file.CmsObject)39 CmsResourceTypeXmlContent (org.opencms.file.types.CmsResourceTypeXmlContent)26 CmsExplorerTypeSettings (org.opencms.workplace.explorer.CmsExplorerTypeSettings)23 CmsProperty (org.opencms.file.CmsProperty)22 CmsUUID (org.opencms.util.CmsUUID)20 CmsLoaderException (org.opencms.loader.CmsLoaderException)19 HashMap (java.util.HashMap)17 CmsVfsResourceNotFoundException (org.opencms.file.CmsVfsResourceNotFoundException)15 Locale (java.util.Locale)14 CmsXmlException (org.opencms.xml.CmsXmlException)12 CmsFile (org.opencms.file.CmsFile)11 CmsXmlContent (org.opencms.xml.content.CmsXmlContent)11 IOException (java.io.IOException)10 Map (java.util.Map)9 CmsADEConfigData (org.opencms.ade.configuration.CmsADEConfigData)9 CmsResourceFilter (org.opencms.file.CmsResourceFilter)8