public static int deleteResource( WebDAVRequest webDAVRequest, String rootPath, String token, long classNameId) throws WebDAVException { try { Resource resource = getResource(webDAVRequest, rootPath, token, classNameId); if (resource == null) { return HttpServletResponse.SC_NOT_FOUND; } Object model = resource.getModel(); if (model instanceof DDMStructure) { DDMStructure structure = (DDMStructure) model; DDMStructureServiceUtil.deleteStructure(structure.getStructureId()); return HttpServletResponse.SC_NO_CONTENT; } else if (model instanceof DDMTemplate) { DDMTemplate template = (DDMTemplate) model; DDMTemplateServiceUtil.deleteTemplate(template.getTemplateId()); return HttpServletResponse.SC_NO_CONTENT; } else { return HttpServletResponse.SC_FORBIDDEN; } } catch (PortalException pe) { return HttpServletResponse.SC_FORBIDDEN; } catch (Exception e) { throw new WebDAVException(e); } }
@Test public void testArticleImages() throws Exception { ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId()); int initialArticleImagesCount = JournalArticleImageLocalServiceUtil.getArticleImagesCount(group.getGroupId()); Class<?> clazz = getClass(); ClassLoader classLoader = clazz.getClassLoader(); String definition = StringUtil.read( classLoader, "com/liferay/journal/dependencies" + "/test-ddm-structure-image-field.xml"); DDMForm ddmForm = DDMFormXSDDeserializerUtil.deserialize(definition); DDMStructure ddmStructure = DDMStructureTestUtil.addStructure( serviceContext.getScopeGroupId(), JournalArticle.class.getName(), ddmForm); DDMTemplate ddmTemplate = DDMTemplateTestUtil.addTemplate( serviceContext.getScopeGroupId(), ddmStructure.getStructureId()); String content = StringUtil.read( classLoader, "com/liferay/journal/dependencies" + "/test-journal-content-image-field.xml"); Map<String, byte[]> images = new HashMap<>(); images.put( "_image_1_0_en_US", FileUtil.getBytes(clazz, "/com/liferay/journal/dependencies/liferay.png")); baseModel = JournalTestUtil.addArticleWithXMLContent( JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, content, ddmStructure.getStructureKey(), ddmTemplate.getTemplateKey(), images, serviceContext); Assert.assertEquals( initialArticleImagesCount + 1, JournalArticleImageLocalServiceUtil.getArticleImagesCount(group.getGroupId())); moveBaseModelToTrash((Long) baseModel.getPrimaryKeyObj()); TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(getBaseModelClassName()); trashHandler.deleteTrashEntry(getTrashEntryClassPK(baseModel)); Assert.assertEquals( initialArticleImagesCount, JournalArticleImageLocalServiceUtil.getArticleImagesCount(group.getGroupId())); }
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 public DDMTemplate copyTemplate(long templateId, ServiceContext serviceContext) throws PortalException, SystemException { DDMDisplay ddmDisplay = DDMUtil.getDDMDisplay(serviceContext); DDMTemplate template = ddmTemplatePersistence.findByPrimaryKey(templateId); long classNameId = template.getClassNameId(); DDMPermission.check( getPermissionChecker(), serviceContext.getScopeGroupId(), ddmDisplay.getResourceName(classNameId), ddmDisplay.getAddTemplateActionId()); return ddmTemplateLocalService.copyTemplate(getUserId(), templateId, serviceContext); }
public long getPortletDisplayTemplateDDMTemplateId( ThemeDisplay themeDisplay, String displayStyle) { long portletDisplayDDMTemplateId = 0; long portletDisplayDDMTemplateGroupId = getDDMTemplateGroupId(themeDisplay); if (displayStyle.startsWith("ddmTemplate_")) { DDMTemplate portletDisplayDDMTemplate = fetchDDMTemplate(portletDisplayDDMTemplateGroupId, displayStyle); if (portletDisplayDDMTemplate != null) { portletDisplayDDMTemplateId = portletDisplayDDMTemplate.getTemplateId(); } } return portletDisplayDDMTemplateId; }
@Deprecated @Override public long getPortletDisplayTemplateDDMTemplateId(long groupId, String displayStyle) { long portletDisplayDDMTemplateId = 0; long portletDisplayDDMTemplateGroupId = getDDMTemplateGroupId(groupId); if (displayStyle.startsWith(DISPLAY_STYLE_PREFIX)) { DDMTemplate portletDisplayDDMTemplate = fetchDDMTemplate(portletDisplayDDMTemplateGroupId, displayStyle); if (portletDisplayDDMTemplate != null) { portletDisplayDDMTemplateId = portletDisplayDDMTemplate.getTemplateId(); } } return portletDisplayDDMTemplateId; }
public int compareTo(DDMTemplate ddmTemplate) { long primaryKey = ddmTemplate.getPrimaryKey(); if (getPrimaryKey() < primaryKey) { return -1; } else if (getPrimaryKey() > primaryKey) { return 1; } else { return 0; } }
public static Resource toResource( WebDAVRequest webDAVRequest, DDMTemplate template, String rootPath, boolean appendPath) { String parentPath = rootPath + webDAVRequest.getPath(); String name = StringPool.BLANK; if (appendPath) { name = String.valueOf(template.getTemplateId()); } return new DDMTemplateResourceImpl(template, parentPath, name); }
@Override public boolean equals(Object obj) { if (obj == null) { return false; } DDMTemplate ddmTemplate = null; try { ddmTemplate = (DDMTemplate) obj; } catch (ClassCastException cce) { return false; } long primaryKey = ddmTemplate.getPrimaryKey(); if (getPrimaryKey() == primaryKey) { return true; } else { return false; } }
protected DDMTemplate createTemplate(long templateId, String name, String mode, String script) { DDMTemplate template = new DDMTemplateImpl(); template.setTemplateId(templateId); template.setName(name); template.setMode(mode); template.setScript(script); _templates.put(template.getTemplateId(), template); return template; }
/** * Converts the soap model instance into a normal model instance. * * @param soapModel the soap model instance to convert * @return the normal model instance */ public static DDMTemplate toModel(DDMTemplateSoap soapModel) { DDMTemplate model = new DDMTemplateImpl(); model.setUuid(soapModel.getUuid()); model.setTemplateId(soapModel.getTemplateId()); model.setGroupId(soapModel.getGroupId()); model.setCompanyId(soapModel.getCompanyId()); model.setUserId(soapModel.getUserId()); model.setUserName(soapModel.getUserName()); model.setCreateDate(soapModel.getCreateDate()); model.setModifiedDate(soapModel.getModifiedDate()); model.setClassNameId(soapModel.getClassNameId()); model.setClassPK(soapModel.getClassPK()); model.setTemplateKey(soapModel.getTemplateKey()); model.setName(soapModel.getName()); model.setDescription(soapModel.getDescription()); model.setType(soapModel.getType()); model.setMode(soapModel.getMode()); model.setLanguage(soapModel.getLanguage()); model.setScript(soapModel.getScript()); return model; }
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; }
@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); }
public static int putResource( WebDAVRequest webDAVRequest, String rootPath, String token, long classNameId) throws WebDAVException { try { Resource resource = getResource(webDAVRequest, rootPath, token, classNameId); if (resource == null) { return addResource(webDAVRequest, classNameId); } Object model = resource.getModel(); if (model instanceof DDMStructure) { DDMStructure structure = (DDMStructure) model; HttpServletRequest request = webDAVRequest.getHttpServletRequest(); String xsd = StringUtil.read(request.getInputStream()); DDMStructureServiceUtil.updateStructure( structure.getGroupId(), structure.getParentStructureId(), structure.getClassNameId(), structure.getStructureKey(), structure.getNameMap(), structure.getDescriptionMap(), xsd, new ServiceContext()); return HttpServletResponse.SC_CREATED; } else if (model instanceof DDMTemplate) { DDMTemplate template = (DDMTemplate) model; HttpServletRequest request = webDAVRequest.getHttpServletRequest(); String script = StringUtil.read(request.getInputStream()); DDMTemplateServiceUtil.updateTemplate( template.getTemplateId(), template.getNameMap(), template.getDescriptionMap(), template.getType(), template.getMode(), template.getLanguage(), script, template.isCacheable(), template.isSmallImage(), template.getSmallImageURL(), null, new ServiceContext()); return HttpServletResponse.SC_CREATED; } else { return HttpServletResponse.SC_FORBIDDEN; } } catch (PortalException pe) { return HttpServletResponse.SC_FORBIDDEN; } catch (Exception e) { throw new WebDAVException(e); } }
@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); }
protected JournalArticle addArticle(long groupId, Date displayDate, int when, boolean approved) throws Exception { Map<Locale, String> titleMap = new HashMap<Locale, String>(); titleMap.put(LocaleUtil.getDefault(), ServiceTestUtil.randomString()); Map<Locale, String> descriptionMap = new HashMap<Locale, String>(); descriptionMap.put(LocaleUtil.getDefault(), ServiceTestUtil.randomString()); String content = DDMStructureTestUtil.getSampleStructuredContent(); String xsd = DDMStructureTestUtil.getSampleStructureXSD(); DDMStructure ddmStructure = DDMStructureTestUtil.addStructure(groupId, JournalArticle.class.getName(), xsd); DDMTemplate ddmTemplate = DDMTemplateTestUtil.addTemplate(groupId, ddmStructure.getStructureId()); Calendar displayDateCalendar = getCalendar(displayDate, when); ServiceContext serviceContext = ServiceTestUtil.getServiceContext(groupId); if (approved) { serviceContext.setWorkflowAction(WorkflowConstants.ACTION_PUBLISH); } else { serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT); } return JournalArticleLocalServiceUtil.addArticle( TestPropsValues.getUserId(), groupId, JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID, JournalArticleConstants.CLASSNAME_ID_DEFAULT, 0, StringPool.BLANK, true, JournalArticleConstants.VERSION_DEFAULT, titleMap, descriptionMap, content, "general", ddmStructure.getStructureKey(), ddmTemplate.getTemplateKey(), null, displayDateCalendar.get(Calendar.MONTH), displayDateCalendar.get(Calendar.DAY_OF_MONTH), displayDateCalendar.get(Calendar.YEAR), displayDateCalendar.get(Calendar.HOUR_OF_DAY), displayDateCalendar.get(Calendar.MINUTE), 0, 0, 0, 0, 0, true, 0, 0, 0, 0, 0, true, true, false, null, null, null, null, serviceContext); }
protected void exportImportJournalArticle(boolean companyScopeDependencies) throws Exception { JournalArticle article = null; DDMStructure ddmStructure = null; DDMTemplate ddmTemplate = null; long groupId = group.getGroupId(); Company company = CompanyLocalServiceUtil.fetchCompany(group.getCompanyId()); Group companyGroup = company.getGroup(); if (companyScopeDependencies) { groupId = companyGroup.getGroupId(); } ddmStructure = DDMStructureTestUtil.addStructure(groupId, JournalArticle.class.getName()); ddmTemplate = DDMTemplateTestUtil.addTemplate(groupId, ddmStructure.getStructureId()); String content = DDMStructureTestUtil.getSampleStructuredContent(); article = JournalTestUtil.addArticleWithXMLContent( group.getGroupId(), content, ddmStructure.getStructureKey(), ddmTemplate.getTemplateKey()); exportImportPortlet(PortletKeys.JOURNAL); int articlesCount = JournalArticleLocalServiceUtil.getArticlesCount(importedGroup.getGroupId()); Assert.assertEquals(1, articlesCount); JournalArticle groupArticle = JournalArticleLocalServiceUtil.fetchJournalArticleByUuidAndGroupId( article.getUuid(), importedGroup.getGroupId()); Assert.assertNotNull(groupArticle); groupId = importedGroup.getGroupId(); if (companyScopeDependencies) { DDMStructure importedDDMStructure = DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId( ddmStructure.getUuid(), groupId); Assert.assertNull(importedDDMStructure); DDMTemplate importedDDMTemplate = DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId( ddmTemplate.getUuid(), groupId); Assert.assertNull(importedDDMTemplate); groupId = companyGroup.getGroupId(); } DDMStructure dependentDDMStructure = DDMStructureLocalServiceUtil.fetchDDMStructureByUuidAndGroupId( ddmStructure.getUuid(), groupId); Assert.assertNotNull(dependentDDMStructure); DDMTemplate dependentDDMTemplate = DDMTemplateLocalServiceUtil.fetchDDMTemplateByUuidAndGroupId( ddmTemplate.getUuid(), groupId); Assert.assertNotNull(dependentDDMTemplate); Assert.assertEquals(article.getDDMStructureKey(), dependentDDMStructure.getStructureKey()); Assert.assertEquals(article.getDDMTemplateKey(), dependentDDMTemplate.getTemplateKey()); Assert.assertEquals(dependentDDMTemplate.getClassPK(), dependentDDMStructure.getStructureId()); }