protected DDMTemplate addTemplate( long userId, long groupId, DDMTemplate template, long classPK, File smallFile, ServiceContext serviceContext) throws Exception { DDMTemplate newTemplate = null; try { return DDMTemplateLocalServiceUtil.addTemplate( userId, groupId, template.getClassNameId(), classPK, template.getTemplateKey(), template.getNameMap(), template.getDescriptionMap(), template.getType(), template.getMode(), template.getLanguage(), template.getScript(), template.isCacheable(), template.isSmallImage(), template.getSmallImageURL(), smallFile, serviceContext); } catch (TemplateDuplicateTemplateKeyException tdtke) { newTemplate = DDMTemplateLocalServiceUtil.addTemplate( userId, groupId, template.getClassNameId(), classPK, null, template.getNameMap(), template.getDescriptionMap(), template.getType(), template.getMode(), template.getLanguage(), template.getScript(), template.isCacheable(), template.isSmallImage(), template.getSmallImageURL(), smallFile, serviceContext); if (_log.isWarnEnabled()) { _log.warn( "A template with the key " + template.getTemplateKey() + " already exists. The new generated key is " + newTemplate.getTemplateKey()); } } return newTemplate; }
public String renderDDMTemplate( PageContext pageContext, long ddmTemplateId, List<?> entries, Map<String, Object> contextObjects) throws Exception { contextObjects.put(PortletDisplayTemplateConstants.DDM_TEMPLATE_ID, ddmTemplateId); contextObjects.put(PortletDisplayTemplateConstants.ENTRIES, entries); if (entries.size() == 1) { contextObjects.put(PortletDisplayTemplateConstants.ENTRY, entries.get(0)); } HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); contextObjects.put(PortletDisplayTemplateConstants.LOCALE, request.getLocale()); contextObjects.put(PortletDisplayTemplateConstants.REQUEST, request); RenderRequest renderRequest = (RenderRequest) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST); contextObjects.put(PortletDisplayTemplateConstants.RENDER_REQUEST, renderRequest); RenderResponse renderResponse = (RenderResponse) request.getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE); contextObjects.put(PortletDisplayTemplateConstants.RENDER_RESPONSE, renderResponse); PortletURL currentURL = PortletURLUtil.getCurrent(renderRequest, renderResponse); contextObjects.put(PortletDisplayTemplateConstants.CURRENT_URL, currentURL.toString()); ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY); contextObjects.put(PortletDisplayTemplateConstants.THEME_DISPLAY, themeDisplay); // Taglibs DDMTemplate ddmTemplate = DDMTemplateLocalServiceUtil.getTemplate(ddmTemplateId); String language = ddmTemplate.getLanguage(); if (language.equals(TemplateConstants.LANG_TYPE_FTL)) { _addTaglibSupportFTL(contextObjects, pageContext); } else if (language.equals(TemplateConstants.LANG_TYPE_VM)) { _addTaglibSupportVM(contextObjects, pageContext); } contextObjects.putAll(_getPortletPreferences(renderRequest)); return _transformer.transform(themeDisplay, contextObjects, ddmTemplate.getScript(), language); }
@Override protected void doImportStagedModel(PortletDataContext portletDataContext, DDMTemplate template) throws Exception { long userId = portletDataContext.getUserId(template.getUserUuid()); Map<Long, Long> structureIds = (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(DDMStructure.class); long classPK = MapUtil.getLong(structureIds, template.getClassPK(), template.getClassPK()); File smallFile = null; if (template.isSmallImage()) { Element element = portletDataContext.getImportDataStagedModelElement(template); String smallImagePath = element.attributeValue("small-image-path"); if (Validator.isNotNull(template.getSmallImageURL())) { String smallImageURL = JournalPortletDataHandler.importReferencedContent( portletDataContext, element, template.getSmallImageURL()); template.setSmallImageURL(smallImageURL); } else if (Validator.isNotNull(smallImagePath)) { byte[] bytes = portletDataContext.getZipEntryAsByteArray(smallImagePath); if (bytes != null) { smallFile = FileUtil.createTempFile(template.getSmallImageType()); FileUtil.write(smallFile, bytes); } } } ServiceContext serviceContext = portletDataContext.createServiceContext(template, DDMPortletDataHandler.NAMESPACE); DDMTemplate importedTemplate = null; if (portletDataContext.isDataStrategyMirror()) { DDMTemplate existingTemplate = DDMTemplateUtil.fetchByUUID_G(template.getUuid(), portletDataContext.getScopeGroupId()); if (existingTemplate == null) { serviceContext.setUuid(template.getUuid()); importedTemplate = addTemplate( userId, portletDataContext.getScopeGroupId(), template, classPK, smallFile, serviceContext); } else { importedTemplate = DDMTemplateLocalServiceUtil.updateTemplate( existingTemplate.getTemplateId(), template.getNameMap(), template.getDescriptionMap(), template.getType(), template.getMode(), template.getLanguage(), template.getScript(), template.isCacheable(), template.isSmallImage(), template.getSmallImageURL(), smallFile, serviceContext); } } else { importedTemplate = addTemplate( userId, portletDataContext.getScopeGroupId(), template, classPK, smallFile, serviceContext); } portletDataContext.importClassedModel( template, importedTemplate, DDMPortletDataHandler.NAMESPACE); }
@Override protected void doExportStagedModel(PortletDataContext portletDataContext, DDMTemplate template) throws Exception { Element dlFileEntryTypesElement = portletDataContext.getExportDataGroupElement(DLFileEntryType.class); Element dlFoldersElement = portletDataContext.getExportDataGroupElement(DLFolder.class); Element dlFileEntriesElement = portletDataContext.getExportDataGroupElement(DLFileEntry.class); Element dlFileRanksElement = portletDataContext.getExportDataGroupElement(DLFileRank.class); Element dlRepositoriesElement = portletDataContext.getExportDataGroupElement(Repository.class); Element dlRepositoryEntriesElement = portletDataContext.getExportDataGroupElement(RepositoryEntry.class); Element templateElement = portletDataContext.getExportDataStagedModelElement(template); if (template.isSmallImage()) { Image smallImage = ImageUtil.fetchByPrimaryKey(template.getSmallImageId()); if (Validator.isNotNull(template.getSmallImageURL())) { String smallImageURL = DDMPortletDataHandler.exportReferencedContent( portletDataContext, dlFileEntryTypesElement, dlFoldersElement, dlFileEntriesElement, dlFileRanksElement, dlRepositoriesElement, dlRepositoryEntriesElement, templateElement, template.getSmallImageURL().concat(StringPool.SPACE)); template.setSmallImageURL(smallImageURL); } else if (smallImage != null) { String smallImagePath = StagedModelPathUtil.getPath( template, smallImage.getImageId() + StringPool.PERIOD + template.getSmallImageType()); templateElement.addAttribute("small-image-path", smallImagePath); template.setSmallImageType(smallImage.getType()); portletDataContext.addZipEntry(smallImagePath, smallImage.getTextObj()); } } if (portletDataContext.getBooleanParameter( DDMPortletDataHandler.NAMESPACE, "embedded-assets")) { String content = DDMPortletDataHandler.exportReferencedContent( portletDataContext, dlFileEntryTypesElement, dlFoldersElement, dlFileEntriesElement, dlFileRanksElement, dlRepositoriesElement, dlRepositoryEntriesElement, templateElement, template.getScript()); template.setScript(content); } portletDataContext.addClassedModel( templateElement, StagedModelPathUtil.getPath(template), template, DDMPortletDataHandler.NAMESPACE); }
@Override public String renderDDMTemplate( HttpServletRequest request, HttpServletResponse response, DDMTemplate ddmTemplate, List<?> entries, Map<String, Object> contextObjects) throws Exception { Transformer transformer = TransformerHolder.getTransformer(); contextObjects.put(PortletDisplayTemplateConstants.TEMPLATE_ID, ddmTemplate.getTemplateId()); contextObjects.put(PortletDisplayTemplateConstants.ENTRIES, entries); if (!entries.isEmpty()) { contextObjects.put(PortletDisplayTemplateConstants.ENTRY, entries.get(0)); } contextObjects.put(PortletDisplayTemplateConstants.LOCALE, request.getLocale()); contextObjects.put(PortletDisplayTemplateConstants.REQUEST, request); RenderRequest renderRequest = (RenderRequest) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST); contextObjects.put(PortletDisplayTemplateConstants.RENDER_REQUEST, renderRequest); RenderResponse renderResponse = (RenderResponse) request.getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE); contextObjects.put(PortletDisplayTemplateConstants.RENDER_RESPONSE, renderResponse); if ((renderRequest != null) && (renderResponse != null)) { PortletURL currentURL = PortletURLUtil.getCurrent(renderRequest, renderResponse); contextObjects.put(PortletDisplayTemplateConstants.CURRENT_URL, currentURL.toString()); } ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); contextObjects.put(PortletDisplayTemplateConstants.THEME_DISPLAY, themeDisplay); // Custom context objects contextObjects.put(TemplateConstants.CLASS_NAME_ID, ddmTemplate.getClassNameId()); String language = ddmTemplate.getLanguage(); TemplateManager templateManager = TemplateManagerUtil.getTemplateManager(language); TemplateHandler templateHandler = TemplateHandlerRegistryUtil.getTemplateHandler(ddmTemplate.getClassNameId()); templateManager.addContextObjects(contextObjects, templateHandler.getCustomContextObjects()); // Taglibs templateManager.addTaglibApplication( contextObjects, PortletDisplayTemplateConstants.FREEMARKER_SERVLET_APPLICATION, request.getServletContext()); templateManager.addTaglibRequest( contextObjects, PortletDisplayTemplateConstants.FREEMARKER_SERVLET_REQUEST, request, response); templateManager.addTaglibFactory( contextObjects, PortletDisplayTemplateConstants.TAGLIB_LIFERAY_HASH, request.getServletContext()); UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(); templateManager.addTaglibTheme( contextObjects, "taglibLiferay", request, new PipingServletResponse(response, unsyncStringWriter)); contextObjects.put(TemplateConstants.WRITER, unsyncStringWriter); if (renderRequest != null) { contextObjects.putAll(_getPortletPreferences(renderRequest)); } return transformer.transform( themeDisplay, contextObjects, ddmTemplate.getScript(), language, unsyncStringWriter); }