@Override
  public void prepareLocalizedFieldsForImport(Locale defaultImportLocale) throws LocaleException {

    super.prepareLocalizedFieldsForImport(defaultImportLocale);

    Locale ddmStructureDefaultLocale = LocaleUtil.fromLanguageId(getDefaultLanguageId());

    try {
      setXsd(
          DDMXMLUtil.updateXMLDefaultLocale(
              getXsd(), ddmStructureDefaultLocale, defaultImportLocale));
    } catch (Exception e) {
      throw new LocaleException(e);
    }
  }
  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);
    }
  }
  public JournalTemplate addTemplate(
      long userId,
      long groupId,
      String templateId,
      boolean autoTemplateId,
      String structureId,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      String xsl,
      boolean formatXsl,
      String langType,
      boolean cacheable,
      boolean smallImage,
      String smallImageURL,
      File smallImageFile,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Template

    User user = userPersistence.findByPrimaryKey(userId);
    templateId = templateId.trim().toUpperCase();
    Date now = new Date();

    try {
      if (formatXsl) {
        if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
          xsl = JournalUtil.formatVM(xsl);
        } else {
          xsl = DDMXMLUtil.formatXML(xsl);
        }
      }
    } catch (Exception e) {
      throw new TemplateXslException();
    }

    byte[] smallImageBytes = null;

    try {
      smallImageBytes = FileUtil.getBytes(smallImageFile);
    } catch (IOException ioe) {
    }

    validate(
        groupId,
        templateId,
        autoTemplateId,
        nameMap,
        xsl,
        smallImage,
        smallImageURL,
        smallImageFile,
        smallImageBytes);

    if (autoTemplateId) {
      templateId = String.valueOf(counterLocalService.increment());
    }

    long id = counterLocalService.increment();

    JournalTemplate template = journalTemplatePersistence.create(id);

    template.setUuid(serviceContext.getUuid());
    template.setGroupId(groupId);
    template.setCompanyId(user.getCompanyId());
    template.setUserId(user.getUserId());
    template.setUserName(user.getFullName());
    template.setCreateDate(serviceContext.getCreateDate(now));
    template.setModifiedDate(serviceContext.getModifiedDate(now));
    template.setTemplateId(templateId);
    template.setStructureId(structureId);
    template.setNameMap(nameMap);
    template.setDescriptionMap(descriptionMap);
    template.setXsl(xsl);
    template.setLangType(langType);
    template.setCacheable(cacheable);
    template.setSmallImage(smallImage);
    template.setSmallImageId(counterLocalService.increment());
    template.setSmallImageURL(smallImageURL);

    journalTemplatePersistence.update(template, false);

    // Resources

    if (serviceContext.getAddGroupPermissions() || serviceContext.getAddGuestPermissions()) {

      addTemplateResources(
          template,
          serviceContext.getAddGroupPermissions(),
          serviceContext.getAddGuestPermissions());
    } else {
      addTemplateResources(
          template, serviceContext.getGroupPermissions(), serviceContext.getGuestPermissions());
    }

    // Expando

    ExpandoBridge expandoBridge = template.getExpandoBridge();

    expandoBridge.setAttributes(serviceContext);

    // Small image

    saveImages(smallImage, template.getSmallImageId(), smallImageFile, smallImageBytes);

    return template;
  }
  protected String getDefultXsd() throws Exception {
    String xsd = readText("test-journal-structure-all-fields.xml");

    return DDMXMLUtil.formatXML(xsd);
  }
  public JournalTemplate updateTemplate(
      long groupId,
      String templateId,
      String structureId,
      Map<Locale, String> nameMap,
      Map<Locale, String> descriptionMap,
      String xsl,
      boolean formatXsl,
      String langType,
      boolean cacheable,
      boolean smallImage,
      String smallImageURL,
      File smallImageFile,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Template

    templateId = templateId.trim().toUpperCase();

    try {
      if (formatXsl) {
        if (langType.equals(JournalTemplateConstants.LANG_TYPE_VM)) {
          xsl = JournalUtil.formatVM(xsl);
        } else {
          xsl = DDMXMLUtil.formatXML(xsl);
        }
      }
    } catch (Exception e) {
      throw new TemplateXslException();
    }

    byte[] smallImageBytes = null;

    try {
      smallImageBytes = FileUtil.getBytes(smallImageFile);
    } catch (IOException ioe) {
    }

    validate(nameMap, xsl, smallImage, smallImageURL, smallImageFile, smallImageBytes);

    JournalTemplate template = journalTemplatePersistence.findByG_T(groupId, templateId);

    template.setModifiedDate(new Date());

    if (Validator.isNull(template.getStructureId()) && Validator.isNotNull(structureId)) {

      // Allow users to set the structure if and only if it currently
      // does not have one. Otherwise, you can have bad data because there
      // may be an existing article that has chosen to use a structure and
      // template combination that no longer exists.

      template.setStructureId(structureId);
    }

    template.setNameMap(nameMap);
    template.setDescriptionMap(descriptionMap);
    template.setXsl(xsl);
    template.setLangType(langType);
    template.setCacheable(cacheable);
    template.setSmallImage(smallImage);
    template.setSmallImageURL(smallImageURL);
    template.setModifiedDate(serviceContext.getModifiedDate(null));

    journalTemplatePersistence.update(template, false);

    // Expando

    ExpandoBridge expandoBridge = template.getExpandoBridge();

    expandoBridge.setAttributes(serviceContext);

    // Small image

    saveImages(smallImage, template.getSmallImageId(), smallImageFile, smallImageBytes);

    return template;
  }