@Override
 public Collection<HistoryByUser> getHistoryByObject(
     ForeignPK foreignPK, int action, String objectType) {
   UserDetail[] allUsers =
       OrganizationControllerProvider.getOrganisationController()
           .getAllUsers(foreignPK.getInstanceId());
   return getHistoryByObject(foreignPK, action, objectType, allUsers);
 }
 /** Test of getMainSessionController method, of class SilverpeasWebUtil. */
 @Test
 public void checkDefaultOrganizationController() {
   OrganizationControllerProvider.getFactory().clearFactory();
   SilverpeasWebUtil util = new SilverpeasWebUtil();
   assertEquals(
       DefaultOrganizationController.class.getName(),
       util.getOrganisationController().getClass().getName());
 }
 /**
  * Gets the name of the creator or of each validators of the specified version of the holded
  * attachment. If the version isn't well defined (it is a dummy version or the document
  * corresponding to the version is empty), then the creator of the holded attachment iself is
  * returned.
  *
  * @param document a version of the holded attachment.
  * @return the display name of the creator or of the validators. In the case of validators, each
  *     of their name is separated by a comma.
  */
 public String getAuthorFullName() {
   String author = document.getUpdatedBy();
   if (!StringUtil.isDefined(author)) {
     author = document.getCreatedBy();
   }
   return OrganizationControllerProvider.getOrganisationController()
       .getUserDetail(author)
       .getDisplayedName();
 }
  @Before
  public void setUp() throws Exception {
    OrganizationControllerProvider.getFactory().clearFactory();

    // Spring
    context = new ClassPathXmlApplicationContext("spring-webComponentManager.xml");
    SilverStatisticsManager.setInstanceForTest(mock(SilverStatisticsManager.class));
    reset(getOrganisationController());
  }
 @Override
 public Collection<HistoryByUser> getHistoryByObject(
     ForeignPK foreignPK, int action, String objectType, List<String> userIds) {
   if (userIds == null || userIds.isEmpty()) {
     return getHistoryByObject(foreignPK, action, objectType);
   }
   UserDetail[] users =
       OrganizationControllerProvider.getOrganisationController()
           .getUserDetails(userIds.toArray(new String[userIds.size()]));
   return getHistoryByObject(foreignPK, action, objectType, users);
 }
  private String printBreadCrumb() {
    StringBuilder result = new StringBuilder();
    String information = getExtraInformation();
    String path = getPath();
    // print javascript to go to spaces in displayed path
    result.append(printScript());
    if (!StringUtil.isDefined(getSpaceJavascriptCallback())) {
      setSpaceJavascriptCallback("goSpace");
    }
    result.append("<div id=\"breadCrumb\">");

    boolean emptyBreadCrumb = true;

    // Display spaces path from root to component
    String language =
        (getMainSessionController() == null)
            ? ""
            : getMainSessionController().getFavoriteLanguage();
    if (StringUtil.isDefined(getComponentId()) || StringUtil.isDefined(getSpaceId())) {
      List<SpaceInst> spaces;

      OrganizationController organizationController =
          OrganizationControllerProvider.getOrganisationController();
      if (StringUtil.isDefined(getComponentId())) {
        spaces = organizationController.getSpacePathToComponent(getComponentId());
      } else {
        spaces = organizationController.getSpacePath(getSpaceId());
      }
      boolean firstSpace = true;
      for (SpaceInst spaceInst : spaces) {
        String spaceId = spaceInst.getId();
        if (!spaceId.startsWith("WA")) {
          spaceId = "WA" + spaceId;
        }
        String href = "javascript:" + getSpaceJavascriptCallback() + "('" + spaceId + "')";
        if (!isClickable()) {
          href = "#";
        }

        if (!firstSpace) {
          result.append(CONNECTOR);
        }
        result.append("<a href=\"").append(href).append("\"");
        result.append(" class=\"space\"");
        result.append(" id=\"space").append(spaceId).append("\"");
        result.append(">");
        result.append(Encode.forHtml(spaceInst.getName(language)));
        result.append("</a>");

        firstSpace = false;
        emptyBreadCrumb = false;
      }

      if (StringUtil.isDefined(getComponentId())) {
        // Display component's label
        ComponentInstLight componentInstLight =
            organizationController.getComponentInstLight(getComponentId());
        if (componentInstLight != null) {
          result.append(CONNECTOR);
          result.append("<a href=\"");
          if (!isClickable()) {
            result.append("#");
          } else if (StringUtil.isDefined(getComponentJavascriptCallback())) {
            result
                .append("javascript:")
                .append(getComponentJavascriptCallback())
                .append("('")
                .append(getComponentId())
                .append("')");
          } else {
            result
                .append(URLUtil.getApplicationURL())
                .append(URLUtil.getURL(getSpaceId(), getComponentId()));
            if (ignoreComponentLink()) {
              result.append("Main");
            } else {
              result.append(getComponentLink());
            }
          }
          result.append("\"");
          result.append(" class=\"component\"");
          result.append(" id=\"bc_").append(componentInstLight.getId()).append("\"");
          result.append(">");
          result.append(Encode.forHtml(componentInstLight.getLabel(language)));
          result.append("</a>");
          emptyBreadCrumb = false;
        }
      }
    } else {
      if (getDomainName() != null) {
        result.append(getDomainName());
        emptyBreadCrumb = false;
      }
      if (getComponentName() != null) {
        if (getDomainName() != null) {
          result.append(CONNECTOR);
        }
        if (getComponentLink() != null) {
          result
              .append("<a href=\"")
              .append(getComponentLink())
              .append("\">")
              .append(getComponentName())
              .append("</a>");
        } else {
          result.append(getComponentName());
        }
        emptyBreadCrumb = false;
      }
    }

    // Display path
    List<BrowseBarElement> elements = getElements();
    if (!elements.isEmpty()) {
      for (BrowseBarElement element : elements) {
        if (!emptyBreadCrumb) {
          result.append(CONNECTOR);
        }
        result.append("<a href=\"").append(element.getLink()).append("\"");
        result.append(" class=\"element\"");
        if (StringUtil.isDefined(element.getId())) {
          result.append(" id=\"").append(element.getId()).append("\"");
        }
        result.append(">");
        result.append(EncodeHelper.javaStringToHtmlString(element.getLabel()));
        result.append("</a>");
        emptyBreadCrumb = false;
      }
    } else if (StringUtil.isDefined(path)) {
      if (!emptyBreadCrumb) {
        result.append(CONNECTOR);
      }
      result.append("<span class=\"path\">");
      result.append(path);
      result.append("</span>");
    }

    // Display extra information
    if (StringUtil.isDefined(information)) {
      if (!emptyBreadCrumb) {
        result.append(CONNECTOR);
      }
      result.append("<span class=\"information\">");
      result.append(information);
      result.append("</span>");
    }

    result.append("</div>");

    return result.toString();
  }
  private Collection<HistoryByUser> getHistoryByObject(
      ForeignPK foreignPK, int action, String objectType, UserDetail[] users) {
    SilverTrace.info(
        "statistic", "DefaultStatisticService.getHistoryByObject()", "root.MSG_GEN_ENTER_METHOD");
    Collection<HistoryObjectDetail> list;
    try {
      list = getHistoryByAction(foreignPK, action, objectType);
    } catch (Exception e) {
      throw new StatisticRuntimeException(
          "DefaultStatisticService.getHistoryByObject()",
          SilverpeasRuntimeException.ERROR,
          "statistic.EX_IMPOSSIBLE_DOBTENIR_LETAT_DES_LECTURES",
          e);
    }
    String[] readerIds = new String[list.size()];
    Date[] date = new Date[list.size()];
    Iterator<HistoryObjectDetail> it = list.iterator();
    int i = 0;
    while (it.hasNext()) {
      HistoryObjectDetail historyObject = it.next();
      readerIds[i] = historyObject.getUserId();
      date[i] = historyObject.getDate();
      i++;
    }
    UserDetail[] controlledUsers =
        OrganizationControllerProvider.getOrganisationController().getUserDetails(readerIds);

    // ajouter à la liste "allUsers" (liste des users des rôles) les users ayant lu mais ne faisant
    // pas partis d'un rôle
    int compteur = 0;
    Collection<UserDetail> allUsers = new ArrayList<>(users.length + controlledUsers.length);
    for (int j = 0; j < users.length; j++) {
      allUsers.add(users[j]);
      compteur = j + 1;
    }
    for (int j = compteur; j < controlledUsers.length; j++) {
      if (!allUsers.contains(controlledUsers[j])) {
        allUsers.add(controlledUsers[j]);
      }
    }

    // création de la liste de tous les utilisateur ayant le droit de lecture
    Collection<HistoryByUser> statByUser = new ArrayList<>(allUsers.size());
    for (UserDetail user : allUsers) {
      if (user != null) {
        HistoryByUser historyByUser = new HistoryByUser(user, null, 0);
        statByUser.add(historyByUser);
      }
    }

    // création d'une liste des accès par utilisateur
    Map<UserDetail, Date> byUser = new HashMap<>(controlledUsers.length);
    Map<UserDetail, Integer> nbAccessbyUser = new HashMap<>(controlledUsers.length);
    for (int j = 0; j < controlledUsers.length; j++) {
      if (controlledUsers[j] != null) {
        // regarder si la date en cours est > à la date enregistrée...
        Object obj = byUser.get(controlledUsers[j]);
        if (obj != null && !obj.toString().equals("Never")) {
          Date dateTab = (Date) obj;
          if (date[j].after(dateTab)) {
            byUser.put(controlledUsers[j], date[j]);
          }
          Object objNb = nbAccessbyUser.get(controlledUsers[j]);
          int nbAccess = 0;
          if (objNb != null) {
            nbAccess = (Integer) objNb;
            nbAccess = nbAccess + 1;
          }
          nbAccessbyUser.put(controlledUsers[j], nbAccess);
        } else {
          byUser.put(controlledUsers[j], date[j]);
          nbAccessbyUser.put(controlledUsers[j], 1);
        }
      }
    }

    // mise à jour de la date de dernier accès et du nombre d'accès pour les utilisateurs ayant lu
    for (final HistoryByUser historyByUser : statByUser) {
      UserDetail user = historyByUser.getUser();
      // recherche de la date de dernier accès
      Date lastAccess = byUser.get(user);
      if (lastAccess != null) {
        historyByUser.setLastAccess(lastAccess);
      }
      // retrieve access number
      Integer nbAccess = nbAccessbyUser.get(user);
      if (nbAccess != null) {
        historyByUser.setNbAccess(nbAccess);
      }
    }

    // Sort list to get readers first
    LastAccessComparatorDesc comparator = new LastAccessComparatorDesc();
    Collections.sort((List<HistoryByUser>) statByUser, comparator);

    SilverTrace.info(
        "statistic", "DefaultStatisticService.getHistoryByObject()", "root.MSG_GEN_EXIT_METHOD");
    return statByUser;
  }
 @After
 public void tearDown() {
   OrganizationControllerProvider.getFactory().clearFactory();
   SilverStatisticsManager.setInstanceForTest(null);
   context.close();
 }