public JournalStructure copyStructure(
      long userId,
      long groupId,
      String oldStructureId,
      String newStructureId,
      boolean autoStructureId)
      throws PortalException, SystemException {

    // Structure

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

    JournalStructure oldStructure = journalStructurePersistence.findByG_S(groupId, oldStructureId);

    if (autoStructureId) {
      newStructureId = String.valueOf(counterLocalService.increment());
    } else {
      validateStructureId(newStructureId);

      JournalStructure newStructure =
          journalStructurePersistence.fetchByG_S(groupId, newStructureId);

      if (newStructure != null) {
        throw new DuplicateStructureIdException();
      }
    }

    long id = counterLocalService.increment();

    JournalStructure newStructure = journalStructurePersistence.create(id);

    newStructure.setGroupId(groupId);
    newStructure.setCompanyId(user.getCompanyId());
    newStructure.setUserId(user.getUserId());
    newStructure.setUserName(user.getFullName());
    newStructure.setCreateDate(now);
    newStructure.setModifiedDate(now);
    newStructure.setStructureId(newStructureId);
    newStructure.setName(oldStructure.getName());
    newStructure.setDescription(oldStructure.getDescription());
    newStructure.setXsd(oldStructure.getXsd());

    journalStructurePersistence.update(newStructure, false);

    // Resources

    addStructureResources(newStructure, true, true);

    return newStructure;
  }
  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;
  }