/* (non-Javadoc)
   * @see org.sakaiproject.entity.api.EntityTransferrer#transferCopyEntities(java.lang.String, java.lang.String, java.util.List, boolean)
   */
  public void transferCopyEntities(
      String fromContext, String toContext, List ids, boolean cleanup) {
    // determine report definitions to copy
    List<ReportDef> list = null;
    if (ids != null && ids.size() > 0) {
      list = new ArrayList<ReportDef>();
      for (String id : (List<String>) ids) {
        ReportDef rd = M_rm.getReportDefinition(Long.valueOf(id));
        if (rd != null) {
          list.add(rd);
        }
      }
    } else {
      list = M_rm.getReportDefinitions(fromContext, false, true);
    }

    // cleanup existing reports on destination site before copying
    if (cleanup) {
      List<ReportDef> listToCleanUp = M_rm.getReportDefinitions(toContext, false, true);
      for (ReportDef rd : listToCleanUp) {
        M_rm.removeReportDefinition(rd);
      }
    }

    // copy to destination
    for (ReportDef rd : list) {
      rd.setId(0);
      rd.setSiteId(toContext);
      rd.getReportParams().setSiteId(toContext);
      M_rm.saveReportDefinition(rd);
    }
  }
  public void testReportExporting() {
    String siteId = null;
    Report r = null;
    ReportParams rp = null;
    List<String> totalsBy = null;

    siteId = FakeData.SITE_A_ID;
    ReportDef rd = new ReportDef();
    rd.setId(0);
    rd.setSiteId(siteId);
    rd.setCreatedBy(FakeData.USER_A_ID);
    rd.setModifiedBy(FakeData.USER_A_ID);
    rd.setTitle("Title 1");
    rp = new ReportParams(siteId);
    rp.setWhat(ReportManager.WHAT_RESOURCES);
    rp.setWhen(ReportManager.WHEN_ALL);
    rp.setWho(ReportManager.WHO_ALL);
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_SITE);
    totalsBy.add(StatsManager.T_USER);
    rp.setHowTotalsBy(totalsBy);
    rp.setHowSort(false);
    rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
    rd.setReportParams(rp);
    Report report = M_rm.getReport(rd, false);
    assertNotNull(report);

    // CSV
    String csv = M_rm.getReportAsCsv(report);
    assertNotNull(csv);
    assertTrue(csv.length() > 0);

    // EXCEL
    byte[] excel = M_rm.getReportAsExcel(report, "sheetname");
    assertNotNull(excel);
    assertTrue(excel.length > 0);
    // To verify locally...
    //		File file = new File("d:/sitestats-test.xls");
    //		if(file.exists()) {file.delete();}
    //		FileOutputStream out = null;
    //		try{
    //			out = new FileOutputStream(file);
    //			out.write(excel);
    //			out.flush();
    //		}catch(FileNotFoundException e){
    //			e.printStackTrace();
    //		}catch(IOException e){
    //			e.printStackTrace();
    //		}finally{
    //			if(out != null) {
    //				try{ out.close(); }catch(IOException e){ /* IGNORE */}
    //			}
    //		}

    // PDF: currently disabled due to classloading trouble
    //		byte[] pdf = M_rm.getReportAsPDF(report);
    //		assertNotNull(pdf);
    //		assertTrue(pdf.length > 0);
  }
 private void checkCollumns(ReportParams params) {
   List<String> all =
       Arrays.asList(
           StatsManager.T_SITE,
           StatsManager.T_USER,
           StatsManager.T_EVENT,
           StatsManager.T_TOOL,
           StatsManager.T_RESOURCE,
           StatsManager.T_RESOURCE_ACTION,
           StatsManager.T_DATE,
           StatsManager.T_DATEMONTH,
           StatsManager.T_DATEYEAR,
           StatsManager.T_LASTDATE,
           StatsManager.T_TOTAL,
           StatsManager.T_VISITS,
           StatsManager.T_UNIQUEVISITS,
           StatsManager.T_DURATION);
   List<String> totalsBy = params.getHowTotalsBy();
   for (String c : all) {
     boolean containsColumn = M_rm.isReportColumnAvailable(params, c);
     boolean expected =
         totalsBy == null
             || totalsBy.contains(c)
             || (c.equals(StatsManager.T_TOTAL)
                 && !ReportManager.WHAT_PRESENCES.equals(params.getWhat()))
             || (c.equals(StatsManager.T_DURATION)
                 && ReportManager.WHAT_PRESENCES.equals(params.getWhat()));
     // System.out.println("containsColumn("+c+"): "+containsColumn+" expected: "+expected);
     assertEquals(expected, containsColumn);
   }
 }
  protected void generateReport(Report report) throws SAXException {
    if (report == null) {
      throw new NullPointerException("Parameter report must not be null");
    }
    if (handler == null) {
      throw new IllegalStateException("ContentHandler not set");
    }

    handler.startElement("report");

    String reportTitle = M_rm.getReportFormattedParams().getReportTitle(report);
    if (reportTitle != null && reportTitle.trim().length() != 0) {
      reportTitle =
          msgs.getString("reportres_title_detailed").replaceAll("\\$\\{title\\}", reportTitle);
    } else {
      reportTitle = msgs.getString("reportres_title");
    }
    handler.element("title", reportTitle);

    // description
    String reportDescription = M_rm.getReportFormattedParams().getReportDescription(report);
    if (reportDescription != null) {
      generateReportSummaryHeaderRow(
          msgs.getString("reportres_summ_description"), reportDescription);
    }
    // site
    String reportSite = M_rm.getReportFormattedParams().getReportSite(report);
    if (reportSite != null) {
      generateReportSummaryHeaderRow(msgs.getString("reportres_summ_site"), reportSite);
    }
    // activity based on
    generateReportSummaryHeaderRow(
        msgs.getString("reportres_summ_act_basedon"),
        M_rm.getReportFormattedParams().getReportActivityBasedOn(report));
    String reportResourceAction = M_rm.getReportFormattedParams().getReportResourceAction(report);
    // resources action
    if (reportResourceAction != null)
      generateReportSummaryHeaderRow(
          M_rm.getReportFormattedParams().getReportResourceActionTitle(report),
          reportResourceAction);
    // activity selection
    String reportActivitySelection =
        M_rm.getReportFormattedParams().getReportActivitySelection(report);
    if (reportActivitySelection != null)
      generateReportSummaryHeaderRow(
          M_rm.getReportFormattedParams().getReportActivitySelectionTitle(report),
          reportActivitySelection);
    // time period
    generateReportSummaryHeaderRow(
        msgs.getString("reportres_summ_timeperiod"),
        M_rm.getReportFormattedParams().getReportTimePeriod(report));
    // user selection type
    generateReportSummaryHeaderRow(
        msgs.getString("reportres_summ_usr_selectiontype"),
        M_rm.getReportFormattedParams().getReportUserSelectionType(report));
    // user selection
    String reportUserSelection = M_rm.getReportFormattedParams().getReportUserSelection(report);
    if (reportUserSelection != null)
      generateReportSummaryHeaderRow(
          M_rm.getReportFormattedParams().getReportUserSelectionTitle(report), reportUserSelection);
    // report timestamp
    generateReportSummaryHeaderRow(
        msgs.getString("reportres_summ_generatedon"),
        M_rm.getReportFormattedParams().getReportGenerationDate(report));

    // set column display info
    setColumnDisplayInfo(report.getReportDefinition().getReportParams());

    // display chart and/or table?
    /*ReportParams params = report.getReportDefinition().getReportParams();
        boolean showChart = ReportManager.HOW_PRESENTATION_BOTH.equals(params.getHowPresentationMode())
    || ReportManager.HOW_PRESENTATION_CHART.equals(params.getHowPresentationMode());
        boolean showTable = ReportManager.HOW_PRESENTATION_BOTH.equals(params.getHowPresentationMode())
    || ReportManager.HOW_PRESENTATION_TABLE.equals(params.getHowPresentationMode());*/
    boolean showChart = false;
    boolean showTable = true;
    handler.element("showChart", String.valueOf(showChart));
    handler.element("showTable", String.valueOf(showTable));

    // report chart
    if (showChart) {
      // TODO Embbed image in fop
      generateReportChart(report);
    }

    // report table
    if (showTable) {
      generateReportDataHeader(report.getReportDefinition().getReportParams());
      generateReportTable(report.getReportData(), report.getReportDefinition().getReportParams());
    }

    handler.endElement("report");
  }
  private void generateReportTable(List<Stat> data, ReportParams params) throws SAXException {
    if (data == null || params == null) {
      throw new NullPointerException("Parameter 'data', 'params' must not be null");
    }
    if (handler == null) {
      throw new IllegalStateException("ContentHandler not set");
    }

    final Map<String, ToolInfo> eventIdToolMap = M_ers.getEventIdToolMap();

    boolean showSite = M_rm.isReportColumnAvailable(params, StatsManager.T_SITE);
    boolean showUser = M_rm.isReportColumnAvailable(params, StatsManager.T_USER);
    boolean showTool = M_rm.isReportColumnAvailable(params, StatsManager.T_TOOL);
    boolean showEvent = M_rm.isReportColumnAvailable(params, StatsManager.T_EVENT);
    boolean showResource = M_rm.isReportColumnAvailable(params, StatsManager.T_RESOURCE);
    boolean showResourceAction =
        M_rm.isReportColumnAvailable(params, StatsManager.T_RESOURCE_ACTION);
    boolean showDate =
        M_rm.isReportColumnAvailable(params, StatsManager.T_DATE)
            || M_rm.isReportColumnAvailable(params, StatsManager.T_DATEMONTH)
            || M_rm.isReportColumnAvailable(params, StatsManager.T_DATEYEAR);
    boolean showLastDate = M_rm.isReportColumnAvailable(params, StatsManager.T_LASTDATE);
    boolean showTotal = M_rm.isReportColumnAvailable(params, StatsManager.T_TOTAL);
    boolean showTotalVisits = M_rm.isReportColumnAvailable(params, StatsManager.T_VISITS);
    boolean showTotalUnique = M_rm.isReportColumnAvailable(params, StatsManager.T_UNIQUEVISITS);
    boolean showDuration = M_rm.isReportColumnAvailable(params, StatsManager.T_DURATION);

    Iterator<Stat> i = data.iterator();
    while (i.hasNext()) {
      Stat cs = i.next();
      handler.startElement("datarow");

      // set column display info
      setColumnDisplayInfo(params);

      if (showSite) {
        String siteId = cs.getSiteId();
        String site = null;
        try {
          site = M_ss.getSite(siteId).getTitle();
        } catch (IdUnusedException e) {
          site = msgs.getString("site_unknown");
        }
        handler.element("site", site);
      }
      if (showUser) {
        String userId = null;
        String userName = null;
        String id = cs.getUserId();
        if (id != null) {
          if (("-").equals(id)) {
            userId = "-";
            userName = msgs.getString("user_anonymous");
          } else if (("?").equals(id)) {
            userId = "-";
            userName = msgs.getString("user_anonymous_access");
          } else {
            try {
              User user = M_uds.getUser(id);
              userId = user.getDisplayId();
              userName = M_sm.getUserNameForDisplay(user);
            } catch (UserNotDefinedException e1) {
              userId = id;
              userName = msgs.getString("user_unknown");
            }
          }
        } else {
          userName = msgs.getString("user_unknown");
        }
        handler.element("userid", userId);
        handler.element("username", userName);
      }
      if (showTool) {
        EventStat es = (EventStat) cs;
        String toolId = es.getToolId();
        handler.element("tool", M_ers.getToolName(toolId == null ? "" : toolId));
        handler.element("showToolIcon", "true");
        handler.element("toolicon", "sitestats://" + M_ers.getToolIcon(toolId));
      }
      if (showEvent) {
        EventStat es = (EventStat) cs;
        String eventRef = es.getEventId();
        handler.element("event", M_ers.getEventName(eventRef == null ? "" : eventRef));
        ToolInfo toolInfo = eventIdToolMap.get(eventRef);
        if (toolInfo != null && !showTool) {
          handler.element("showToolEventIcon", "true");
          String toolId = toolInfo.getToolId();
          handler.element("tooleventicon", "sitestats://" + M_ers.getToolIcon(toolId));
        } else {
          handler.element("showToolEventIcon", "false");
        }
      }
      if (showResource) {
        ResourceStat rs = (ResourceStat) cs;
        String resName = M_sm.getResourceName(rs.getResourceRef());
        handler.element("resource", resName == null ? "" : resName);
        handler.element(
            "resourceimg",
            "library://" + M_sm.getResourceImageLibraryRelativePath(rs.getResourceRef()));
      }
      if (showResourceAction) {
        ResourceStat rs = (ResourceStat) cs;
        String resAction = rs.getResourceAction();
        handler.element("action", resAction == null ? "" : msgs.getString("action_" + resAction));
      }
      if (showDate) {
        java.util.Date date = cs.getDate();
        if (M_rm.isReportColumnAvailable(params, StatsManager.T_DATE)) {
          handler.element(
              "date", date == null ? "" : M_ts.newTime(date.getTime()).toStringLocalDate());
        } else if (M_rm.isReportColumnAvailable(params, StatsManager.T_DATEMONTH)) {
          handler.element("date", date == null ? "" : dateMonthFrmt.format(date));
        } else if (M_rm.isReportColumnAvailable(params, StatsManager.T_DATEYEAR)) {
          handler.element("date", date == null ? "" : dateYearFrmt.format(date));
        }
      }
      if (showLastDate) {
        java.util.Date date = cs.getDate();
        handler.element(
            "lastdate", date == null ? "" : M_ts.newTime(date.getTime()).toStringLocalDate());
      }
      if (showTotal) {
        handler.element("total", String.valueOf(cs.getCount()));
      }
      if (showTotalVisits) {
        SiteVisits ss = (SiteVisits) cs;
        handler.element("totalVisits", String.valueOf(ss.getTotalVisits()));
      }
      if (showTotalUnique) {
        SiteVisits ss = (SiteVisits) cs;
        handler.element("totalUnique", String.valueOf(ss.getTotalUnique()));
      }
      if (showDuration) {
        SitePresence ss = (SitePresence) cs;
        double durationInMin =
            ss.getDuration() == 0
                ? 0
                : Util.round((double) ss.getDuration() / 1000 / 60, 1); // in minutes
        handler.element("duration", String.valueOf(durationInMin));
      }

      handler.endElement("datarow");
    }

    // empty report
    if (data.size() == 0) {
      String messageNoData = msgs.getString("no_data");

      handler.startElement("datarow");
      // set column display info
      setColumnDisplayInfo(params);

      if (showSite) {
        handler.element("site", messageNoData);
        messageNoData = "";
      }
      if (showUser) {
        handler.element("userid", messageNoData);
        messageNoData = "";
        handler.element("username", messageNoData);
      }
      if (showTool) {
        handler.element("tool", messageNoData);
        messageNoData = "";
        handler.element("showToolIcon", "");
        handler.element("toolicon", "");
      }
      if (showEvent) {
        handler.element("event", messageNoData);
        messageNoData = "";
        handler.element("showToolEventIcon", "false");
      }
      if (showResource) {
        handler.element("resource", messageNoData);
        messageNoData = "";
        handler.element("resourceimg", "");
      }
      if (showResourceAction) {
        handler.element("action", messageNoData);
        messageNoData = "";
      }
      if (showDate) {
        handler.element("date", messageNoData);
        messageNoData = "";
      }
      if (showLastDate) {
        handler.element("lastdate", messageNoData);
        messageNoData = "";
      }
      if (showTotal) {
        handler.element("total", messageNoData);
        messageNoData = "";
      }
      if (showTotalVisits) {
        handler.element("totalVisits", messageNoData);
        messageNoData = "";
      }
      if (showTotalUnique) {
        handler.element("totalUnique", messageNoData);
        messageNoData = "";
      }
      if (showDuration) {
        handler.element("duration", messageNoData);
        messageNoData = "";
      }
      handler.endElement("datarow");
    }
  }
 private void setColumnDisplayInfo(ReportParams params) throws SAXException {
   if (handler == null) {
     throw new IllegalStateException("ContentHandler not set");
   }
   handler.element("what", params.getWhat());
   handler.element("who", params.getWho());
   handler.element(
       "showSite", String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_SITE)));
   handler.element(
       "showUser", String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_USER)));
   handler.element(
       "showTool", String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_TOOL)));
   handler.element(
       "showEvent", String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_EVENT)));
   handler.element(
       "showResource",
       String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_RESOURCE)));
   handler.element(
       "showResourceAction",
       String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_RESOURCE_ACTION)));
   handler.element(
       "showDate",
       String.valueOf(
           M_rm.isReportColumnAvailable(params, StatsManager.T_DATE)
               || M_rm.isReportColumnAvailable(params, StatsManager.T_DATEMONTH)
               || M_rm.isReportColumnAvailable(params, StatsManager.T_DATEYEAR)));
   handler.element(
       "showLastDate",
       String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_LASTDATE)));
   handler.element(
       "showTotal", String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_TOTAL)));
   handler.element(
       "showTotalVisits",
       String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_VISITS)));
   handler.element(
       "showTotalUnique",
       String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_UNIQUEVISITS)));
   handler.element(
       "showDuration",
       String.valueOf(M_rm.isReportColumnAvailable(params, StatsManager.T_DURATION)));
 }
  public void testReportsFromOverviewPage() {
    M_sum.collectEvents(getSampleData2());

    // MiniStatsVisits & MiniStatUniqueVisits
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      rp.setWhat(ReportManager.WHAT_VISITS_TOTALS);
      rp.setWhen(ReportManager.WHEN_ALL);
      rp.setWho(ReportManager.WHO_ALL);
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_DATE);
      totalsBy.add(StatsManager.T_VISITS);
      totalsBy.add(StatsManager.T_UNIQUEVISITS);
      rp.setHowTotalsBy(totalsBy);
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_DATE);
      rp.setHowSortAscending(false);
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_BOTH);
      rp.setHowChartType(StatsManager.CHARTTYPE_TIMESERIESBAR);
      rp.setHowChartSource(StatsManager.T_DATE);
      rp.setHowChartSeriesSource(StatsManager.T_NONE);
      rp.setHowChartSeriesPeriod(StatsManager.CHARTTIMESERIES_DAY);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(5, rep.getReportData().size());
    }

    // MiniStatEnrolledUsersWithVisits
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      rp.setWhat(ReportManager.WHAT_VISITS);
      rp.setWhen(ReportManager.WHEN_ALL);
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_USER);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(2, rep.getReportData().size());
    }

    // MiniStatEnrolledUsersWithoutVisits
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      rp.setWhat(ReportManager.WHAT_VISITS);
      rp.setWhen(ReportManager.WHEN_ALL);
      rp.setWho(ReportManager.WHO_NONE);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_USER);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(0, rep.getReportData().size());
    }

    // MiniStatActivityEvents
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_EVENTS);
      rp.setWhatEventSelType(ReportManager.WHAT_EVENTS_BYEVENTS);
      rp.setWhatEventIds(new ArrayList<String>(M_ers.getEventIds()));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_EVENT);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_EVENT);
      rp.setHowSortAscending(true);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(6, rep.getReportData().size());
    }

    // MiniStatMostActiveUser
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_EVENTS);
      rp.setWhatEventSelType(ReportManager.WHAT_EVENTS_BYEVENTS);
      rp.setWhatEventIds(new ArrayList<String>(M_ers.getEventIds()));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_USER);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_TOTAL);
      rp.setHowSortAscending(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(3, rep.getReportData().size());
    }

    // MiniStatFiles (files with new event)
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_RESOURCES);
      rp.setWhatLimitedAction(true);
      rp.setWhatResourceAction(ReportManager.WHAT_RESOURCES_ACTION_NEW);
      rp.setWhatLimitedResourceIds(true);
      rp.setWhatResourceIds(Arrays.asList(StatsManager.RESOURCES_DIR + FakeData.SITE_A_ID + "/"));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_RESOURCE);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_RESOURCE);
      rp.setHowSortAscending(true);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(2, rep.getReportData().size());
    }

    // MiniStatOpenedFiles (files with read event)
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_RESOURCES);
      rp.setWhatLimitedAction(true);
      rp.setWhatResourceAction(ReportManager.WHAT_RESOURCES_ACTION_READ);
      rp.setWhatLimitedResourceIds(true);
      rp.setWhatResourceIds(Arrays.asList(StatsManager.RESOURCES_DIR + FakeData.SITE_A_ID + "/"));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_RESOURCE);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_TOTAL);
      rp.setHowSortAscending(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(2, rep.getReportData().size());
    }

    // MiniStatUserThatOpenedMoreFiles
    {
      ReportDef r = new ReportDef();
      r.setId(0);
      r.setSiteId(FakeData.SITE_A_ID);
      ReportParams rp = new ReportParams(FakeData.SITE_A_ID);
      // what
      rp.setWhat(ReportManager.WHAT_RESOURCES);
      rp.setWhatLimitedAction(true);
      rp.setWhatResourceAction(ReportManager.WHAT_RESOURCES_ACTION_READ);
      rp.setWhatLimitedResourceIds(true);
      rp.setWhatResourceIds(Arrays.asList(StatsManager.RESOURCES_DIR + FakeData.SITE_A_ID + "/"));
      // when
      rp.setWhen(ReportManager.WHEN_ALL);
      // who
      rp.setWho(ReportManager.WHO_ALL);
      // grouping
      List<String> totalsBy = new ArrayList<String>();
      totalsBy.add(StatsManager.T_USER);
      rp.setHowTotalsBy(totalsBy);
      // sorting
      rp.setHowSort(true);
      rp.setHowSortBy(StatsManager.T_TOTAL);
      rp.setHowSortAscending(false);
      // chart
      rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
      r.setReportParams(rp);
      Report rep = M_rm.getReport(r, false);
      assertNotNull(rep);
      assertEquals(2, rep.getReportData().size());
    }
  }
  public void testGetMoreReports() {
    M_sum.collectEvents(getSampleData());
    String siteId = null;
    Report r = null;
    ReportDef rd = null;
    ReportParams rp = null;
    List<String> totalsBy = null;

    // resources
    siteId = FakeData.SITE_A_ID;
    rd = new ReportDef();
    rd.setId(0);
    rd.setSiteId(siteId);
    rp = new ReportParams(siteId);
    rp.setWhat(ReportManager.WHAT_RESOURCES);
    rp.setWhen(ReportManager.WHEN_ALL);
    rp.setWho(ReportManager.WHO_ALL);
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_SITE);
    totalsBy.add(StatsManager.T_USER);
    rp.setHowTotalsBy(totalsBy);
    rp.setHowSort(false);
    rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
    rd.setReportParams(rp);
    r = M_rm.getReport(rd, true, null, false);
    checkCollumns(rd.getReportParams());
    assertEquals(1, r.getReportData().size());

    // visits
    rp.setWhat(ReportManager.WHAT_VISITS);
    r = M_rm.getReport(rd, true, null, true);
    checkCollumns(rd.getReportParams());
    assertEquals(2, r.getReportData().size());

    // visits totals
    rp.setWhat(ReportManager.WHAT_VISITS_TOTALS);
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_SITE);
    rp.setHowTotalsBy(totalsBy);
    rd.setId(1);
    r = M_rm.getReport(rd, true, new PagingPosition(0, 5), true);
    assertEquals(1, r.getReportData().size());
    assertEquals(9, ((SiteVisits) (r.getReportData().get(0))).getTotalVisits());
    assertEquals(7, ((SiteVisits) (r.getReportData().get(0))).getTotalUnique());

    //		// activity totals
    //		rp.setWhat(ReportManager.WHAT_ACTIVITY_TOTALS);
    //		rp.setWhatEventIds(FakeData.EVENTIDS);
    //		rp.setWhen(ReportManager.WHEN_LAST365DAYS);
    //		r = M_rm.getReport(rd, false, null, false);
    //		System.out.println(r.getReportData());
    //		System.out.println("ReportParams: "+ rp);
    //		System.out.println("ReportData: "+ r.getReportData());
    //		assertEquals(1, r.getReportData().size());
    //		assertEquals(1, r.getReportData().get(0).getCount());

    // presences I
    rp.setWhat(ReportManager.WHAT_PRESENCES);
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_SITE);
    totalsBy.add(StatsManager.T_USER);
    totalsBy.add(StatsManager.T_DATE);
    rp.setHowTotalsBy(totalsBy);
    rp.setHowSort(false);
    r = M_rm.getReport(rd, true, null, true);
    checkCollumns(rd.getReportParams());
    assertEquals(7, r.getReportData().size());

    // presences II
    rp.setWhat(ReportManager.WHAT_PRESENCES);
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_SITE);
    totalsBy.add(StatsManager.T_DATE);
    rp.setHowTotalsBy(totalsBy);
    rp.setHowSort(false);
    r = M_rm.getReport(rd, true, null, true);
    checkCollumns(rd.getReportParams());
    assertEquals(5, r.getReportData().size());

    // presences III
    rp.setWhat(ReportManager.WHAT_PRESENCES);
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_SITE);
    rp.setHowTotalsBy(totalsBy);
    rp.setHowSort(false);
    r = M_rm.getReport(rd, true, null, true);
    checkCollumns(rd.getReportParams());
    assertEquals(1, r.getReportData().size());
  }
  // ---- TESTS ----
  public void testGetReport() {
    M_sum.collectEvents(getSampleData());
    String siteId = null;
    Report r = null;
    ReportDef rd = null;
    ReportParams rp = null;
    List<String> totalsBy = null;

    // #1 getReport(ReportDef reportDef, boolean restrictToToolsInSite)
    siteId = FakeData.SITE_B_ID;
    rd = new ReportDef();
    rd.setId(0);
    rd.setSiteId(siteId);
    rp = new ReportParams(siteId);
    rp.setWhat(ReportManager.WHAT_EVENTS);
    rp.setWhatEventSelType(ReportManager.WHAT_EVENTS_BYTOOL);
    rp.setWhatToolIds(Arrays.asList(ReportManager.WHAT_EVENTS_ALLTOOLS));
    rp.setWhen(ReportManager.WHEN_ALL);
    rp.setWho(ReportManager.WHO_ALL);
    // grouping
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_TOOL);
    totalsBy.add(StatsManager.T_DATE);
    rp.setHowTotalsBy(totalsBy);
    // sorting
    rp.setHowSort(true);
    rp.setHowSortBy(StatsManager.T_TOOL);
    rp.setHowSortAscending(false);
    // chart
    rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_BOTH);
    rp.setHowChartType(StatsManager.CHARTTYPE_TIMESERIESBAR);
    rp.setHowChartSource(StatsManager.T_DATE);
    rp.setHowChartSeriesSource(StatsManager.T_NONE);
    rp.setHowChartSeriesPeriod(StatsManager.CHARTTIMESERIES_DAY);
    rd.setReportParams(rp);
    r = M_rm.getReport(rd, true);
    checkCollumns(rd.getReportParams());
    assertEquals(1, r.getReportData().size());
    r = M_rm.getReport(rd, false);
    checkCollumns(rd.getReportParams());
    assertEquals(2, r.getReportData().size());
    assertNotNull(M_rm.getReportFormattedParams());

    // #2 getReportRowCount(ReportDef reportDef, boolean restrictToToolsInSite)
    assertEquals(1, M_rm.getReportRowCount(rd, true));
    assertEquals(2, M_rm.getReportRowCount(rd, false));

    siteId = FakeData.SITE_B_ID;
    rd = new ReportDef();
    rd.setId(0);
    rd.setSiteId(siteId);
    rp = new ReportParams(siteId);
    rp.setWhat(ReportManager.WHAT_RESOURCES);
    rp.setWhen(ReportManager.WHEN_ALL);
    rp.setWho(ReportManager.WHO_ALL);
    // grouping
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_RESOURCE);
    totalsBy.add(StatsManager.T_DATE);
    rp.setHowTotalsBy(totalsBy);
    // sorting
    rp.setHowSort(true);
    rp.setHowSortBy(StatsManager.T_TOOL);
    rp.setHowSortAscending(false);
    // chart
    rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
    rd.setReportParams(rp);
    assertEquals(1, M_rm.getReportRowCount(rd, true));
    assertEquals(1, M_rm.getReportRowCount(rd, false));
  }
Exemple #10
0
  public void testLoadSaveReports() {
    String siteId = null;
    Report r = null;
    ReportParams rp = null;
    List<String> totalsBy = null;

    siteId = FakeData.SITE_A_ID;
    ReportDef rd = new ReportDef();
    rd.setId(0);
    rd.setSiteId(siteId);
    rd.setCreatedBy(FakeData.USER_A_ID);
    rd.setModifiedBy(FakeData.USER_A_ID);
    rd.setTitle("Title 1");
    rp = new ReportParams(siteId);
    rp.setWhat(ReportManager.WHAT_RESOURCES);
    rp.setWhen(ReportManager.WHEN_ALL);
    rp.setWho(ReportManager.WHO_ALL);
    totalsBy = new ArrayList<String>();
    totalsBy.add(StatsManager.T_SITE);
    totalsBy.add(StatsManager.T_USER);
    rp.setHowTotalsBy(totalsBy);
    rp.setHowSort(false);
    rp.setHowPresentationMode(ReportManager.HOW_PRESENTATION_TABLE);
    rd.setReportParams(rp);

    // non-existent
    try {
      assertNull(M_rm.getReportDefinition(100));
    } catch (Exception e) {
      assertTrue(true);
    }
    // normal
    assertTrue(M_rm.saveReportDefinition(rd));
    assertNotNull(M_rm.getReportDefinition(1));
    // hidden
    ReportDef rd2 = new ReportDef();
    rd2.setId(0);
    rd2.setSiteId(siteId);
    rd2.setCreatedBy(FakeData.USER_A_ID);
    rd2.setModifiedBy(FakeData.USER_A_ID);
    rd2.setTitle("Title 2");
    rd2.setHidden(true);
    rd2.setReportParams(new ReportParams());
    assertTrue(M_rm.saveReportDefinition(rd2));
    // pre-defined
    ReportDef rd3 = new ReportDef();
    rd3.setId(0);
    rd3.setSiteId(null);
    rd3.setCreatedBy(FakeData.USER_A_ID);
    rd3.setModifiedBy(FakeData.USER_A_ID);
    rd3.setTitle("Title 3");
    rd3.setHidden(false);
    rd3.setReportParams(new ReportParams());
    assertTrue(M_rm.saveReportDefinition(rd3));

    List<ReportDef> list = M_rm.getReportDefinitions(null, true, true);
    assertNotNull(list);
    assertEquals(1, list.size());
    list = M_rm.getReportDefinitions(FakeData.SITE_A_ID, true, true);
    assertNotNull(list);
    assertEquals(3, list.size());
    list = M_rm.getReportDefinitions(FakeData.SITE_A_ID, true, false);
    assertNotNull(list);
    assertEquals(2, list.size());
    list = M_rm.getReportDefinitions(FakeData.SITE_A_ID, false, true);
    assertNotNull(list);
    assertEquals(2, list.size());
    list = M_rm.getReportDefinitions(FakeData.SITE_A_ID, false, false);
    assertNotNull(list);
    assertEquals(1, list.size());

    assertTrue(M_rm.removeReportDefinition(rd2));
    list = M_rm.getReportDefinitions(FakeData.SITE_A_ID, true, true);
    assertNotNull(list);
    assertEquals(2, list.size());
  }