예제 #1
0
 private static Authorship createAuthorship(Map<String, String> beanAttr)
     throws InternalErrorException {
   if (beanAttr == null) return null;
   Authorship authorship = new Authorship();
   authorship.setId(Integer.valueOf(beanAttr.get("id")).intValue());
   authorship.setPublicationId(Integer.valueOf(beanAttr.get("publicationId")).intValue());
   authorship.setUserId(Integer.valueOf(beanAttr.get("userId")).intValue());
   authorship.setCreatedBy(BeansUtils.eraseEscaping(beanAttr.get("createdBy")));
   authorship.setCreatedByUid(
       (beanAttr.get("createdByUid").equals("\\0"))
           ? null
           : Integer.valueOf(beanAttr.get("createdByUid")).intValue());
   if (BeansUtils.eraseEscaping(beanAttr.get("createdDate")) == null)
     authorship.setCreatedDate(null);
   else {
     Date date;
     try {
       date =
           BeansUtils.DATE_FORMATTER.parse(BeansUtils.eraseEscaping(beanAttr.get("createdDate")));
     } catch (ParseException ex) {
       throw new InternalErrorException("Error when date was parsing from String to Date.", ex);
     }
     authorship.setCreatedDate(date);
   }
   return authorship;
 }
  @Override
  public Attribute getAttributeValue(
      PerunSessionImpl sess, User user, AttributeDefinition attributeDefinition)
      throws InternalErrorException {
    Attribute attribute = new Attribute(attributeDefinition);
    HashMap<String, String> organizationsWithLoa = new LinkedHashMap<String, String>();

    List<UserExtSource> extSources =
        sess.getPerunBl().getUsersManagerBl().getUserExtSources(sess, user);
    if (extSources == null || extSources.isEmpty())
      return attribute; // If no userExtSources, so no Loa for any of them.

    String version = attributeDefinition.getFriendlyNameParameter();
    if (version == null)
      throw new InternalErrorException(
          "There is no parameter (cs or en) in attribute " + attributeDefinition);

    UserExtSource userExtSourceForCreating = null;
    UserExtSource userExtSourceForModifiing = null;

    // Initialize MapOfExtSource
    initializeMapOfExtSourceName();

    for (UserExtSource uES : extSources) {
      String uEName = uES.getExtSource().getName();
      String uELoa = String.valueOf(uES.getLoa());

      if (uES.getCreatedAt() != null) {
        Date testingDate = null;
        Date lastUsedDate = null;
        boolean parsed = true;
        try {
          testingDate = BeansUtils.DATE_FORMATTER.parse(uES.getCreatedAt());
        } catch (Exception ex) {
          // Not Parsed correctly
          parsed = false;
        }
        if (parsed) {
          if (userExtSourceForCreating == null || userExtSourceForCreating.getCreatedAt() == null)
            userExtSourceForCreating = uES;
          else {
            try {
              lastUsedDate =
                  BeansUtils.DATE_FORMATTER.parse(userExtSourceForCreating.getCreatedAt());
              if (testingDate != null && testingDate.compareTo(lastUsedDate) < 0) {
                userExtSourceForCreating = uES;
              }
            } catch (Exception ex) {
              // Not Parsed correctly
              userExtSourceForCreating = uES;
            }
          }
        }
      }

      if (uES.getModifiedAt() != null) {
        Date testingDate = null;
        Date lastUsedDate = null;
        boolean parsed = true;
        try {
          testingDate = BeansUtils.DATE_FORMATTER.parse(uES.getModifiedAt());
        } catch (Exception ex) {
          // Not Parsed correctly
          parsed = false;
        }
        if (parsed) {
          if (userExtSourceForModifiing == null
              || userExtSourceForModifiing.getModifiedAt() == null) userExtSourceForModifiing = uES;
          else {
            try {
              lastUsedDate =
                  BeansUtils.DATE_FORMATTER.parse(userExtSourceForModifiing.getModifiedAt());
              if (testingDate != null && testingDate.compareTo(lastUsedDate) < 0) {
                userExtSourceForModifiing = uES;
              }
            } catch (Exception ex) {
              // Not Parsed correctly
              userExtSourceForModifiing = uES;
            }
          }
        }
      }

      String uESimpleName = getSimpleNameOfExtSource(uEName, version.equals("cs"));
      organizationsWithLoa.put(uESimpleName, uELoa);
    }

    // Set created,modified by userExtSources
    if (userExtSourceForCreating != null) {
      attribute.setValueCreatedAt(userExtSourceForCreating.getCreatedAt());
      attribute.setValueCreatedBy(userExtSourceForCreating.getCreatedBy());
    }
    if (userExtSourceForModifiing != null) {
      attribute.setValueModifiedAt(userExtSourceForModifiing.getModifiedAt());
      attribute.setValueModifiedBy(userExtSourceForModifiing.getModifiedBy());
    }
    attribute.setValue(organizationsWithLoa);
    return attribute;
  }