protected void fixElementsDefaultLocale( Element element, Locale contentDefaultLocale, Locale contentNewDefaultLocale) { for (Element dynamicElementElement : element.elements(_DYNAMIC_ELEMENT)) { Element importMetaDataElement = (Element) dynamicElementElement.selectSingleNode( "meta-data[@locale='" + contentNewDefaultLocale.toString() + "']"); if (importMetaDataElement == null) { Element metaDataElement = (Element) dynamicElementElement.selectSingleNode( "meta-data[@locale='" + contentDefaultLocale.toString() + "']"); Element copiedMetadataElement = metaDataElement.createCopy(); Attribute localeAttribute = copiedMetadataElement.attribute(_LOCALE); String contentNewDefaultLanguageId = LocaleUtil.toLanguageId(contentNewDefaultLocale); localeAttribute.setValue(contentNewDefaultLanguageId); dynamicElementElement.add(copiedMetadataElement); } fixElementsDefaultLocale( dynamicElementElement, contentDefaultLocale, contentNewDefaultLocale); } }
private ImageSelector _getImageSelector( PortletDataContext portletDataContext, long fileEntryId, List<Element> attachmentElements) throws Exception { FileEntry oldImageFileEntry = PortletFileRepositoryUtil.getPortletFileEntry(fileEntryId); for (Element attachmentElement : attachmentElements) { Attribute uuidAttribute = attachmentElement.attribute("uuid"); String uuidAttributeValue = uuidAttribute.getValue(); if (uuidAttributeValue.equals(oldImageFileEntry.getUuid())) { String path = attachmentElement.attributeValue("path"); FileEntry fileEntry = (FileEntry) portletDataContext.getZipEntryAsObject(path); return new ImageSelector( FileUtil.getBytes(fileEntry.getContentStream()), fileEntry.getFileName(), fileEntry.getMimeType(), null); } } return null; }
protected List<String> readSpecialNamespaces(Element root) throws ImportFilesException { List<String> namespaces = new ArrayList<String>(); Element siteinfoElement = root.element("siteinfo"); if (siteinfoElement == null) { throw new ImportFilesException("Invalid pages XML file"); } Element namespacesElement = siteinfoElement.element("namespaces"); List<Element> namespaceElements = namespacesElement.elements("namespace"); for (Element namespaceElement : namespaceElements) { Attribute attribute = namespaceElement.attribute("key"); String value = attribute.getValue(); if (!value.equals("0")) { namespaces.add(namespaceElement.getText()); } } return namespaces; }
@Override public String updateXMLDefaultLocale( String xml, Locale contentDefaultLocale, Locale contentNewDefaultLocale) { try { if (LocaleUtil.equals(contentDefaultLocale, contentNewDefaultLocale)) { return xml; } Document document = SAXReaderUtil.read(xml); Element rootElement = document.getRootElement(); Attribute availableLocalesAttribute = rootElement.attribute(_AVAILABLE_LOCALES); String contentNewDefaultLanguageId = LocaleUtil.toLanguageId(contentNewDefaultLocale); String availableLocalesAttributeValue = availableLocalesAttribute.getValue(); if (!availableLocalesAttributeValue.contains(contentNewDefaultLanguageId)) { StringBundler sb = new StringBundler(3); sb.append(availableLocalesAttribute.getValue()); sb.append(StringPool.COMMA); sb.append(contentNewDefaultLanguageId); availableLocalesAttribute.setValue(sb.toString()); } Attribute defaultLocaleAttribute = rootElement.attribute(_DEFAULT_LOCALE); defaultLocaleAttribute.setValue(contentNewDefaultLanguageId); fixElementsDefaultLocale(rootElement, contentDefaultLocale, contentNewDefaultLocale); return document.formattedString(); } catch (DocumentException de) { throw new SystemException(de); } catch (IOException ioe) { throw new SystemException(ioe); } }
protected void setupBridgeTCKSite(long companyId, long userId) throws Exception, DocumentException { Group site = getSite(companyId, userId, "Bridge TCK"); long groupId = site.getGroupId(); addAllUsersToSite(companyId, groupId); URL configFileURL = getClass().getClassLoader().getResource("pluto-portal-driver-config.xml"); Document document = SAXReaderUtil.read(configFileURL); Element rootElement = document.getRootElement(); Element renderConfigElement = rootElement.element("render-config"); Iterator<Element> pageElementIterator = renderConfigElement.elementIterator("page"); while (pageElementIterator.hasNext()) { Element pageElement = pageElementIterator.next(); Attribute nameAttribute = pageElement.attribute("name"); String pageName = nameAttribute.getValue(); Element portletElement = pageElement.element("portlet"); nameAttribute = portletElement.attribute("name"); String portletName = nameAttribute.getValue(); String liferayPortletName = portletName.replaceAll(StringPool.DASH, StringPool.BLANK); String liferayPortletId = liferayPortletName + "_WAR_bridgetckmainportlet"; PortalPage portalPage = new PortalPage(pageName, liferayPortletId); setupPage(userId, groupId, portalPage); } setupPage( userId, groupId, new PortalPage( "Lifecycle Set", "chapter3TestslifecycleTestportlet_WAR_bridgetcklifecyclesetportlet")); setupPage( userId, groupId, new PortalPage( "Render Policy Always Delegate", "chapter3TestsrenderPolicyTestportlet_WAR_bridgetckrenderpolicy1portlet")); setupPage( userId, groupId, new PortalPage( "Render Policy Default", "chapter3TestsrenderPolicyTestportlet_WAR_bridgetckrenderpolicy2portlet")); setupPage( userId, groupId, new PortalPage( "Render Policy Never Delegate", "chapter3TestsrenderPolicyTestportlet_WAR_bridgetckrenderpolicy3portlet")); setupPage( userId, groupId, new PortalPage( "Render Response Wrapper", "chapter6_2_1TestsusesConfiguredRenderResponseWrapperTestportlet_WAR_bridgetckresponsewrapperportlet")); setupPage( userId, groupId, new PortalPage( "Resource Response Wrapper", "chapter6_2_1TestsusesConfiguredResourceResponseWrapperTestportlet_WAR_bridgetckresponsewrapperportlet")); }
protected void addDDMStructures( long userId, long groupId, long classNameId, String fileName, ServiceContext serviceContext) throws Exception { List<Element> structureElements = getDDMStructures(fileName); for (Element structureElement : structureElements) { boolean dynamicStructure = GetterUtil.getBoolean(structureElement.elementText("dynamic-structure")); if (dynamicStructure) { continue; } String name = structureElement.elementText("name"); String description = structureElement.elementText("description"); String ddmStructureKey = name; DDMStructure ddmStructure = DDMStructureLocalServiceUtil.fetchStructure(groupId, ddmStructureKey); if (ddmStructure != null) { continue; } Element structureElementRootElement = structureElement.element("root"); String xsd = structureElementRootElement.asXML(); Map<Locale, String> nameMap = new HashMap<Locale, String>(); nameMap.put(LocaleUtil.getDefault(), name); Map<Locale, String> descriptionMap = new HashMap<Locale, String>(); descriptionMap.put(LocaleUtil.getDefault(), description); Attribute defaultLocaleAttribute = structureElementRootElement.attribute("default-locale"); Locale ddmStructureDefaultLocale = LocaleUtil.fromLanguageId(defaultLocaleAttribute.getValue()); xsd = DDMXMLUtil.updateXMLDefaultLocale( xsd, ddmStructureDefaultLocale, LocaleUtil.getDefault()); DDMStructureLocalServiceUtil.addStructure( userId, groupId, classNameId, ddmStructureKey, nameMap, descriptionMap, xsd, "xml", DDMStructureConstants.TYPE_DEFAULT, serviceContext); } }
protected void doImportLayouts( long userId, long groupId, boolean privateLayout, Map<String, String[]> parameterMap, File file) throws Exception { boolean deleteMissingLayouts = MapUtil.getBoolean( parameterMap, PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS, Boolean.TRUE.booleanValue()); boolean deletePortletData = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.DELETE_PORTLET_DATA); boolean importCategories = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.CATEGORIES); boolean importPermissions = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PERMISSIONS); boolean importPublicLayoutPermissions = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PUBLIC_LAYOUT_PERMISSIONS); boolean importUserPermissions = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.USER_PERMISSIONS); boolean importPortletData = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_DATA); boolean importPortletSetup = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_SETUP); boolean importPortletArchivedSetups = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS); boolean importPortletUserPreferences = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_USER_PREFERENCES); boolean importTheme = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.THEME); boolean importThemeSettings = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.THEME_REFERENCE); boolean importLogo = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.LOGO); boolean importLayoutSetSettings = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.LAYOUT_SET_SETTINGS); boolean layoutSetPrototypeLinkEnabled = MapUtil.getBoolean( parameterMap, PortletDataHandlerKeys.LAYOUT_SET_PROTOTYPE_LINK_ENABLED, true); Group group = GroupLocalServiceUtil.getGroup(groupId); if (group.isLayoutSetPrototype()) { layoutSetPrototypeLinkEnabled = false; } boolean publishToRemote = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PUBLISH_TO_REMOTE); String layoutsImportMode = MapUtil.getString( parameterMap, PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE, PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_UUID); String portletsMergeMode = MapUtil.getString( parameterMap, PortletDataHandlerKeys.PORTLETS_MERGE_MODE, PortletDataHandlerKeys.PORTLETS_MERGE_MODE_REPLACE); String userIdStrategy = MapUtil.getString(parameterMap, PortletDataHandlerKeys.USER_ID_STRATEGY); if (_log.isDebugEnabled()) { _log.debug("Delete portlet data " + deletePortletData); _log.debug("Import categories " + importCategories); _log.debug("Import permissions " + importPermissions); _log.debug("Import user permissions " + importUserPermissions); _log.debug("Import portlet data " + importPortletData); _log.debug("Import portlet setup " + importPortletSetup); _log.debug("Import portlet archived setups " + importPortletArchivedSetups); _log.debug("Import portlet user preferences " + importPortletUserPreferences); _log.debug("Import theme " + importTheme); } StopWatch stopWatch = null; if (_log.isInfoEnabled()) { stopWatch = new StopWatch(); stopWatch.start(); } LayoutCache layoutCache = new LayoutCache(); LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(groupId, privateLayout); long companyId = layoutSet.getCompanyId(); User user = UserUtil.findByPrimaryKey(userId); UserIdStrategy strategy = _portletImporter.getUserIdStrategy(user, userIdStrategy); ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file); PortletDataContext portletDataContext = new PortletDataContextImpl( companyId, groupId, parameterMap, new HashSet<String>(), strategy, zipReader); portletDataContext.setPortetDataContextListener( new PortletDataContextListenerImpl(portletDataContext)); portletDataContext.setPrivateLayout(privateLayout); // Zip Element rootElement = null; InputStream themeZip = null; // Manifest String xml = portletDataContext.getZipEntryAsString("/manifest.xml"); if (xml == null) { throw new LARFileException("manifest.xml not found in the LAR"); } try { Document document = SAXReaderUtil.read(xml); rootElement = document.getRootElement(); } catch (Exception e) { throw new LARFileException(e); } // Build compatibility Element headerElement = rootElement.element("header"); int buildNumber = ReleaseInfo.getBuildNumber(); int importBuildNumber = GetterUtil.getInteger(headerElement.attributeValue("build-number")); if (buildNumber != importBuildNumber) { throw new LayoutImportException( "LAR build number " + importBuildNumber + " does not match " + "portal build number " + buildNumber); } // Type compatibility String larType = headerElement.attributeValue("type"); if (!larType.equals("layout-set") && !larType.equals("layout-set-prototype")) { throw new LARTypeException("Invalid type of LAR file (" + larType + ")"); } // Group id long sourceGroupId = GetterUtil.getLong(headerElement.attributeValue("group-id")); portletDataContext.setSourceGroupId(sourceGroupId); // Layout set prototype if (group.isLayoutSetPrototype() && larType.equals("layout-set-prototype")) { LayoutSetPrototype layoutSetPrototype = LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(group.getClassPK()); String layoutSetPrototypeUuid = GetterUtil.getString(headerElement.attributeValue("type-uuid")); LayoutSetPrototype existingLayoutSetPrototype = null; if (Validator.isNotNull(layoutSetPrototypeUuid)) { try { existingLayoutSetPrototype = LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypeByUuid( layoutSetPrototypeUuid); } catch (NoSuchLayoutSetPrototypeException nslspe) { } } if (existingLayoutSetPrototype == null) { layoutSetPrototype.setUuid(layoutSetPrototypeUuid); LayoutSetPrototypeLocalServiceUtil.updateLayoutSetPrototype(layoutSetPrototype); } } Element layoutsElement = rootElement.element("layouts"); String layoutSetPrototypeUuid = layoutsElement.attributeValue("layout-set-prototype-uuid"); ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext(); if (Validator.isNotNull(layoutSetPrototypeUuid)) { if (layoutSetPrototypeLinkEnabled) { if (publishToRemote) { importLayoutSetPrototype( portletDataContext, user, layoutSetPrototypeUuid, serviceContext); } } layoutSet.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid); layoutSet.setLayoutSetPrototypeLinkEnabled(layoutSetPrototypeLinkEnabled); LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet); } // Look and feel if (importTheme) { themeZip = portletDataContext.getZipEntryAsInputStream("theme.zip"); } // Look and feel String themeId = layoutSet.getThemeId(); String colorSchemeId = layoutSet.getColorSchemeId(); if (importThemeSettings) { Attribute themeIdAttribute = headerElement.attribute("theme-id"); if (themeIdAttribute != null) { themeId = themeIdAttribute.getValue(); } Attribute colorSchemeIdAttribute = headerElement.attribute("color-scheme-id"); if (colorSchemeIdAttribute != null) { colorSchemeId = colorSchemeIdAttribute.getValue(); } } if (importLogo) { String logoPath = headerElement.attributeValue("logo-path"); byte[] iconBytes = portletDataContext.getZipEntryAsByteArray(logoPath); if ((iconBytes != null) && (iconBytes.length > 0)) { File logo = FileUtil.createTempFile(iconBytes); LayoutSetLocalServiceUtil.updateLogo(groupId, privateLayout, true, logo); } else { LayoutSetLocalServiceUtil.updateLogo(groupId, privateLayout, false, (File) null); } } if (importLayoutSetSettings) { String settings = GetterUtil.getString(headerElement.elementText("settings")); LayoutSetLocalServiceUtil.updateSettings(groupId, privateLayout, settings); } String css = GetterUtil.getString(headerElement.elementText("css")); if (themeZip != null) { String importThemeId = importTheme(layoutSet, themeZip); if (importThemeId != null) { themeId = importThemeId; colorSchemeId = ColorSchemeImpl.getDefaultRegularColorSchemeId(); } if (_log.isDebugEnabled()) { _log.debug("Importing theme takes " + stopWatch.getTime() + " ms"); } } boolean wapTheme = false; LayoutSetLocalServiceUtil.updateLookAndFeel( groupId, privateLayout, themeId, colorSchemeId, css, wapTheme); // Read asset categories, asset tags, comments, locks, permissions, and // ratings entries to make them available to the data handlers through // the context if (importPermissions) { _permissionImporter.readPortletDataPermissions(portletDataContext); } if (importCategories) { _portletImporter.readAssetCategories(portletDataContext); } _portletImporter.readAssetTags(portletDataContext); _portletImporter.readComments(portletDataContext); _portletImporter.readExpandoTables(portletDataContext); _portletImporter.readLocks(portletDataContext); _portletImporter.readRatingsEntries(portletDataContext); // Layouts List<Layout> previousLayouts = LayoutUtil.findByG_P(groupId, privateLayout); // Remove layouts that were deleted from the layout set prototype if (Validator.isNotNull(layoutSetPrototypeUuid) && layoutSetPrototypeLinkEnabled) { LayoutSetPrototype layoutSetPrototype = LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypeByUuid(layoutSetPrototypeUuid); Group layoutSetPrototypeGroup = layoutSetPrototype.getGroup(); for (Layout layout : previousLayouts) { String sourcePrototypeLayoutUuid = layout.getSourcePrototypeLayoutUuid(); if (Validator.isNull(layout.getSourcePrototypeLayoutUuid())) { continue; } Layout sourcePrototypeLayout = LayoutUtil.fetchByUUID_G( sourcePrototypeLayoutUuid, layoutSetPrototypeGroup.getGroupId()); if (sourcePrototypeLayout == null) { LayoutLocalServiceUtil.deleteLayout(layout, false, serviceContext); } } } List<Layout> newLayouts = new ArrayList<Layout>(); Set<Long> newLayoutIds = new HashSet<Long>(); Map<Long, Layout> newLayoutsMap = (Map<Long, Layout>) portletDataContext.getNewPrimaryKeysMap(Layout.class); List<Element> layoutElements = layoutsElement.elements("layout"); if (_log.isDebugEnabled()) { if (layoutElements.size() > 0) { _log.debug("Importing layouts"); } } for (Element layoutElement : layoutElements) { importLayout( portletDataContext, user, layoutCache, previousLayouts, newLayouts, newLayoutsMap, newLayoutIds, portletsMergeMode, themeId, colorSchemeId, layoutsImportMode, privateLayout, importPermissions, importPublicLayoutPermissions, importUserPermissions, importThemeSettings, rootElement, layoutElement); } Element portletsElement = rootElement.element("portlets"); List<Element> portletElements = portletsElement.elements("portlet"); // Delete portlet data if (deletePortletData) { if (_log.isDebugEnabled()) { if (portletElements.size() > 0) { _log.debug("Deleting portlet data"); } } for (Element portletElement : portletElements) { String portletId = portletElement.attributeValue("portlet-id"); long layoutId = GetterUtil.getLong(portletElement.attributeValue("layout-id")); long plid = newLayoutsMap.get(layoutId).getPlid(); portletDataContext.setPlid(plid); _portletImporter.deletePortletData(portletDataContext, portletId, plid); } } // Import portlets if (_log.isDebugEnabled()) { if (portletElements.size() > 0) { _log.debug("Importing portlets"); } } for (Element portletElement : portletElements) { String portletPath = portletElement.attributeValue("path"); String portletId = portletElement.attributeValue("portlet-id"); long layoutId = GetterUtil.getLong(portletElement.attributeValue("layout-id")); long plid = newLayoutsMap.get(layoutId).getPlid(); long oldPlid = GetterUtil.getLong(portletElement.attributeValue("old-plid")); Portlet portlet = PortletLocalServiceUtil.getPortletById(portletDataContext.getCompanyId(), portletId); if (!portlet.isActive() || portlet.isUndeployedPortlet()) { continue; } Layout layout = null; try { layout = LayoutUtil.findByPrimaryKey(plid); } catch (NoSuchLayoutException nsle) { continue; } portletDataContext.setPlid(plid); portletDataContext.setOldPlid(oldPlid); Document portletDocument = SAXReaderUtil.read(portletDataContext.getZipEntryAsString(portletPath)); portletElement = portletDocument.getRootElement(); // The order of the import is important. You must always import // the portlet preferences first, then the portlet data, then // the portlet permissions. The import of the portlet data // assumes that portlet preferences already exist. _portletImporter.setPortletScope(portletDataContext, portletElement); try { // Portlet preferences _portletImporter.importPortletPreferences( portletDataContext, layoutSet.getCompanyId(), layout.getGroupId(), layout, null, portletElement, importPortletSetup, importPortletArchivedSetups, importPortletUserPreferences, false); // Portlet data Element portletDataElement = portletElement.element("portlet-data"); if (importPortletData && (portletDataElement != null)) { _portletImporter.importPortletData( portletDataContext, portletId, plid, portletDataElement); } } finally { _portletImporter.resetPortletScope(portletDataContext, layout.getGroupId()); } // Portlet permissions if (importPermissions) { _permissionImporter.importPortletPermissions( layoutCache, companyId, groupId, userId, layout, portletElement, portletId, importUserPermissions); } // Archived setups _portletImporter.importPortletPreferences( portletDataContext, layoutSet.getCompanyId(), groupId, null, null, portletElement, importPortletSetup, importPortletArchivedSetups, importPortletUserPreferences, false); } if (importPermissions) { if ((userId > 0) && ((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) || (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6))) { Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class); indexer.reindex(userId); } } // Asset links _portletImporter.readAssetLinks(portletDataContext); // Delete missing layouts if (deleteMissingLayouts) { deleteMissingLayouts(groupId, privateLayout, newLayoutIds, previousLayouts, serviceContext); } // Page count LayoutSetLocalServiceUtil.updatePageCount(groupId, privateLayout); if (_log.isInfoEnabled()) { _log.info("Importing layouts takes " + stopWatch.getTime() + " ms"); } // Site GroupLocalServiceUtil.updateSite(groupId, true); // Web content layout type for (Layout layout : newLayouts) { UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties(); String articleId = typeSettingsProperties.getProperty("article-id"); if (Validator.isNotNull(articleId)) { Map<String, String> articleIds = (Map<String, String>) portletDataContext.getNewPrimaryKeysMap(JournalArticle.class + ".articleId"); typeSettingsProperties.setProperty( "article-id", MapUtil.getString(articleIds, articleId, articleId)); LayoutUtil.update(layout, false); } } zipReader.close(); }