Пример #1
0
  @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);
  }