public JournalStructure updateStructure(
      long groupId,
      String structureId,
      String parentStructureId,
      String name,
      String description,
      String xsd,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    structureId = structureId.trim().toUpperCase();

    try {
      xsd = JournalUtil.formatXML(xsd);
    } catch (Exception e) {
      throw new StructureXsdException();
    }

    validateParentStructureId(groupId, structureId, parentStructureId);
    validate(name, description, xsd);

    JournalStructure structure = journalStructurePersistence.findByG_S(groupId, structureId);

    structure.setModifiedDate(serviceContext.getModifiedDate(null));
    structure.setParentStructureId(parentStructureId);
    structure.setName(name);
    structure.setDescription(description);
    structure.setXsd(xsd);

    journalStructurePersistence.update(structure, false);

    // Expando

    ExpandoBridge expandoBridge = structure.getExpandoBridge();

    expandoBridge.setAttributes(serviceContext);

    return structure;
  }
  public JournalStructure addStructure(
      long userId,
      long groupId,
      String structureId,
      boolean autoStructureId,
      String parentStructureId,
      String name,
      String description,
      String xsd,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // Structure

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

    try {
      xsd = JournalUtil.formatXML(xsd);
    } catch (Exception e) {
      throw new StructureXsdException();
    }

    if (autoStructureId) {
      structureId = String.valueOf(counterLocalService.increment());
    }

    validate(groupId, structureId, autoStructureId, parentStructureId, name, description, xsd);

    long id = counterLocalService.increment();

    JournalStructure structure = journalStructurePersistence.create(id);

    structure.setUuid(serviceContext.getUuid());
    structure.setGroupId(groupId);
    structure.setCompanyId(user.getCompanyId());
    structure.setUserId(user.getUserId());
    structure.setUserName(user.getFullName());
    structure.setCreateDate(serviceContext.getCreateDate(now));
    structure.setModifiedDate(serviceContext.getModifiedDate(now));
    structure.setStructureId(structureId);
    structure.setParentStructureId(parentStructureId);
    structure.setName(name);
    structure.setDescription(description);
    structure.setXsd(xsd);

    journalStructurePersistence.update(structure, false);

    // Resources

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

      addStructureResources(
          structure,
          serviceContext.getAddCommunityPermissions(),
          serviceContext.getAddGuestPermissions());
    } else {
      addStructureResources(
          structure,
          serviceContext.getCommunityPermissions(),
          serviceContext.getGuestPermissions());
    }

    // Expando

    ExpandoBridge expandoBridge = structure.getExpandoBridge();

    expandoBridge.setAttributes(serviceContext);

    return structure;
  }