private Screen createScreen(String title, ScreenType type) {
   log.info("leads" + leadScreener);
   Screen screen =
       new Screen(
           null,
           Integer.toString(screen_number_counter++),
           leadScreener,
           labHead,
           type,
           StudyType.IN_VITRO,
           ProjectPhase.PRIMARY_SCREEN,
           title);
   screen.addCollaborator(collaborator);
   screen.createStatusItem(new LocalDate(), ScreenStatus.ACCEPTED);
   genericEntityDao.persistEntity(screen);
   return screen;
 }
  public void testFindNewPublishedPrivate() throws Exception {
    initializeData();

    Screen screen1NotPublished = createScreen("SDSL TEST1 Not Published");
    genericEntityDao.persistEntity(screen1NotPublished);

    Screen screen2Published = createScreen("SDSL TEST2 Published");
    screen2Published.createScreenResult();
    Publication publication = new Publication();
    publication.setAuthors("Test Authors");
    publication.setJournal("Test Journal");
    publication.setTitle("Test Publication Title");
    screen2Published.addPublication(publication);
    genericEntityDao.persistEntity(screen2Published);

    Screen screen3Mutual = createScreen("SDSL screen3Mutual");
    screen3Mutual.createScreenResult();
    screen3Mutual.setDataSharingLevel(ScreenDataSharingLevel.MUTUAL_SCREENS);
    publication = new Publication();
    publication.setAuthors("Test Authors x");
    publication.setJournal("Test Journal x");
    publication.setTitle("Test Publication Title x");
    screen3Mutual.addPublication(publication);
    genericEntityDao.persistEntity(screen3Mutual);

    Screen screen4Private = createScreen("SDSL private");
    screen4Private.createScreenResult();
    screen4Private.setDataSharingLevel(ScreenDataSharingLevel.PRIVATE);
    publication = new Publication();
    publication.setAuthors("Test Authors x");
    publication.setJournal("Test Journal x");
    publication.setTitle("Test Publication Title x");
    screen4Private.addPublication(publication);
    genericEntityDao.persistEntity(screen4Private);

    Screen screenPrivatePublishedTransferred =
        createScreen("SDSL screenPrivatePublishedTransferred");
    screenPrivatePublishedTransferred.createScreenResult();
    screenPrivatePublishedTransferred.setDataSharingLevel(ScreenDataSharingLevel.PRIVATE);
    publication = new Publication();
    publication.setAuthors("Test Authors x");
    publication.setJournal("Test Journal x");
    publication.setTitle("Test Publication Title x");
    screenPrivatePublishedTransferred.createStatusItem(
        new LocalDate(), ScreenStatus.TRANSFERRED_TO_BROAD_INSTITUTE);
    screenPrivatePublishedTransferred.addPublication(publication);
    genericEntityDao.persistEntity(screenPrivatePublishedTransferred);

    Screen screen4Public = createScreen("SDSL screen4Public");
    screen4Public.createScreenResult();
    screen4Public.setDataSharingLevel(ScreenDataSharingLevel.SHARED);
    publication = new Publication();
    publication.setAuthors("Test Authors xx");
    publication.setJournal("Test Journal xx");
    publication.setTitle("Test Publication Title xx");
    screen4Public.addPublication(publication);
    genericEntityDao.persistEntity(screen4Public);

    flushAndClear();

    screen2Published = genericEntityDao.reloadEntity(screen2Published);
    screen3Mutual = genericEntityDao.reloadEntity(screen3Mutual);
    screen4Private = genericEntityDao.reloadEntity(screen4Private);

    Set<Screen> publishedScreens =
        Sets.newHashSet(
            screenDataSharingLevelUpdater.findNewPublishedPrivate(
                ScreenType.SMALL_MOLECULE)); // TODO: Test RNAi as well
    assertNotNull(publishedScreens);
    assertEquals(
        Sets.newHashSet(screen2Published, screen3Mutual, screen4Private), publishedScreens);
  }
  public void testFindExpired() throws Exception {
    initializeData();

    LocalDate date = new LocalDate(new Date());

    screen1NotExpired = createScreen("SDSL TEST1");
    screen1NotExpired.setDataPrivacyExpirationDate(date.plusYears(2));
    genericEntityDao.persistEntity(screen1NotExpired);

    screen2Expired = createScreen("SDSL TEST2");
    screen2Expired.createScreenResult();
    screen2Expired.setDataPrivacyExpirationDate(date);
    genericEntityDao.persistEntity(screen2Expired);

    screen3Expired = createScreen("SDSL TEST3");
    screen3Expired.createScreenResult();
    screen3Expired.setDataPrivacyExpirationDate(date.minusYears(1));
    genericEntityDao.persistEntity(screen3Expired);

    screen9ExpiredMaxDatePassed = createScreen("screen9ExpiredMaxDatePassed");
    screen9ExpiredMaxDatePassed.createScreenResult();
    screen9ExpiredMaxDatePassed.setMinAllowedDataPrivacyExpirationDate(date.minusYears(2));
    screen9ExpiredMaxDatePassed.setMaxAllowedDataPrivacyExpirationDate(date.minusYears(2));
    screen9ExpiredMaxDatePassed.setDataPrivacyExpirationDate(date.minusYears(2));
    genericEntityDao.persistEntity(screen9ExpiredMaxDatePassed);

    screen4Default = createScreen("SDSL TEST4");
    genericEntityDao.persistEntity(screen4Default);

    screen5RnaiExpired = createScreen("Test RNAI expired", ScreenType.RNAI);
    screen5RnaiExpired.setDataPrivacyExpirationDate(date.minusYears(1));
    screen5RnaiExpired.createScreenResult();
    genericEntityDao.persistEntity(screen5RnaiExpired);

    screen6ExpiredNoResults = createScreen("test expired no results", ScreenType.RNAI);
    screen6ExpiredNoResults.setDataPrivacyExpirationDate(date.minusYears(1));
    genericEntityDao.persistEntity(screen6ExpiredNoResults);

    screen7ExpiredDropped = createScreen("test expired dropped technical");
    screen7ExpiredDropped.setDataPrivacyExpirationDate(date.minusYears(1));
    screen7ExpiredDropped.createScreenResult();
    screen7ExpiredDropped.createStatusItem(new LocalDate(), ScreenStatus.DROPPED_TECHNICAL);
    genericEntityDao.persistEntity(screen7ExpiredDropped);

    screen8ExpiredTransferred = createScreen("test expired transferred");
    screen8ExpiredTransferred.setDataPrivacyExpirationDate(date.minusYears(1));
    screen8ExpiredTransferred.createScreenResult();
    screen8ExpiredTransferred.createStatusItem(
        new LocalDate(), ScreenStatus.TRANSFERRED_TO_BROAD_INSTITUTE);
    genericEntityDao.persistEntity(screen8ExpiredTransferred);

    flushAndClear();

    //    startNewTransaction();

    List<Screen> allScreens = genericEntityDao.findAllEntitiesOfType(Screen.class);
    for (Screen screen : allScreens) {
      log.info(
          "allScreens: "
              + screen.getTitle()
              + " , expires: "
              + screen.getDataPrivacyExpirationDate());
    }

    List<Screen> screens =
        screenDataSharingLevelUpdater.findNewExpiredNotNotified(date, ScreenType.SMALL_MOLECULE);
    for (Screen screen : screens) {
      log.info(
          "expiredScreens: "
              + screen.getTitle()
              + " , expires: "
              + screen.getDataPrivacyExpirationDate());
    }
    assertFalse("no expired screens", screens.isEmpty());

    screen1NotExpired = genericEntityDao.reloadEntity(screen1NotExpired);
    screen2Expired = genericEntityDao.reloadEntity(screen2Expired);
    screen3Expired = genericEntityDao.reloadEntity(screen3Expired);
    screen4Default = genericEntityDao.reloadEntity(screen4Default);
    screen5RnaiExpired = genericEntityDao.reloadEntity(screen5RnaiExpired);
    screen6ExpiredNoResults = genericEntityDao.reloadEntity(screen6ExpiredNoResults);
    screen7ExpiredDropped = genericEntityDao.reloadEntity(screen7ExpiredDropped);
    screen8ExpiredTransferred = genericEntityDao.reloadEntity(screen8ExpiredTransferred);
    screen9ExpiredMaxDatePassed = genericEntityDao.reloadEntity(screen9ExpiredMaxDatePassed);

    assertTrue("screen2Expired should be expired: ", screens.contains(screen2Expired));
    assertTrue("screen3Expired should be expired: ", screens.contains(screen3Expired));
    assertTrue(
        "screen9ExpiredMaxDatePassed should be expired: ",
        screens.contains(screen9ExpiredMaxDatePassed));

    assertFalse("screen4Default should not be expired", screens.contains(screen4Default));
    assertFalse("screen1NotExpired should not be expired", screens.contains(screen1NotExpired));
    assertFalse(
        "screen5RnaiExpired should NOT be expired (unless setting screenType=RNAi)",
        screens.contains(screen5RnaiExpired));
    assertFalse(
        "screen6ExpiredNoResults should not be expired", screens.contains(screen6ExpiredNoResults));
    assertFalse(
        "screen7ExpiredDropped should not be expired", screens.contains(screen7ExpiredDropped));
    assertFalse(
        "screen8ExpiredTransferred should not be expired",
        screens.contains(screen8ExpiredTransferred));

    // just a little test here to see if we get a null or an empty list, find none
    screens =
        screenDataSharingLevelUpdater.findNewExpiredNotNotified(
            date.minusYears(20), ScreenType.SMALL_MOLECULE);
    log.info("empty result: " + screens);
    assertNotNull(screens);
    assertTrue(screens.isEmpty());

    screens = screenDataSharingLevelUpdater.findNewExpiredNotNotified(date, ScreenType.RNAI);
    assertFalse("no expired screens", screens.isEmpty());
    assertTrue(
        "only the RNAi screen should be returned (no Small Molecule): " + screens,
        screens.size() == 1);
    assertTrue(
        "screen5RnaiExpired should NOT be expired (unless setting screenType=RNAi)",
        screens.contains(screen5RnaiExpired));
  }
  public void testAdjustDataPrivacyExpirationByActivities() throws Exception {
    initializeData();

    // as of [#2175] expiration services to become notification services only
    // the DPED will be set separately from the ScreenResultImport (the old way).
    // It will now be set by batch process, SCREEN_ACTIVITY_DATA_PRIVACY_EXPIRATION_AGE_DAYS from
    // the last ScreeningActivity date for a screen:
    // Meaning:
    // DPED will be set in one step (this test)
    // DPED will be used in separate step to find and expire screens (next test)

    // 1. create some screens
    Screen screen1NoActivities = createScreen("SDSL TEST1");
    Screen screen2HasActivity = createScreen("SDSL TEST2");
    Screen screen5TransferredHasActivity = createScreen("SDSL TEST5");
    screen5TransferredHasActivity.createStatusItem(
        new LocalDate(), ScreenStatus.TRANSFERRED_TO_BROAD_INSTITUTE);
    Screen screen3RnaiHasActivity = createScreen("SDSL TEST RNAI", ScreenType.RNAI);
    Screen screen4HasNoLibraryScreeningActivity =
        createScreen("screen4HasNoLibraryScreeningActivity");
    Screen screen6MinMaxSet = createScreen("screen6MinMaxSet");

    // 2. add some activities
    LocalDate newActivityDate = new LocalDate();
    screen2HasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen2HasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen2HasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen2HasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    // create a screening for user provided plates
    LibraryScreening ls =
        screen2HasActivity.createLibraryScreening(
            admin, leadScreener, newActivityDate.plusDays(100));
    ls.setForExternalLibraryPlates(true);
    // create a non-library-screening activity too
    screen2HasActivity.createCherryPickRequest(admin, leadScreener, newActivityDate.plusDays(100));

    screen4HasNoLibraryScreeningActivity.createCherryPickRequest(
        admin, leadScreener, newActivityDate.plusDays(100));

    screen3RnaiHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);

    screen5TransferredHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen5TransferredHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen5TransferredHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen5TransferredHasActivity.createLibraryScreening(admin, leadScreener, newActivityDate);
    // create a screening for user provided plates
    ls =
        screen5TransferredHasActivity.createLibraryScreening(
            admin, leadScreener, newActivityDate.plusDays(100));
    ls.setForExternalLibraryPlates(true);
    // 3. add some results
    screen2HasActivity.createScreenResult();
    screen5TransferredHasActivity.createScreenResult();
    screen4HasNoLibraryScreeningActivity.createScreenResult();

    LocalDate maxAllowedDataPrivacyExpirationDate = newActivityDate.minusDays(10);
    LocalDate minAllowedDataPrivacyExpirationDate = newActivityDate.minusDays(20);
    screen6MinMaxSet.setMinAllowedDataPrivacyExpirationDate(minAllowedDataPrivacyExpirationDate);
    screen6MinMaxSet.setMaxAllowedDataPrivacyExpirationDate(maxAllowedDataPrivacyExpirationDate);
    screen6MinMaxSet.createLibraryScreening(admin, leadScreener, newActivityDate);
    screen6MinMaxSet.createScreenResult();

    genericEntityDao.persistEntity(screen1NoActivities);
    genericEntityDao.persistEntity(screen2HasActivity);
    genericEntityDao.persistEntity(screen3RnaiHasActivity);
    genericEntityDao.persistEntity(screen4HasNoLibraryScreeningActivity);
    genericEntityDao.persistEntity(screen5TransferredHasActivity);
    genericEntityDao.persistEntity(screen6MinMaxSet);
    flushAndClear();

    // 4. find the ones with "old" activities (activity age >
    // SCREEN_ACTIVITY_DATA_PRIVACY_EXPIRATION_AGE_DAYS)
    int ageToExpireFromActivityDateInDays = 760;

    ScreenDataSharingLevelUpdater.DataPrivacyAdjustment adjustment =
        screenDataSharingLevelUpdater.adjustDataPrivacyExpirationByActivities(
            ageToExpireFromActivityDateInDays, admin, ScreenType.SMALL_MOLECULE);

    flushAndClear();

    screen1NoActivities = genericEntityDao.reloadEntity(screen1NoActivities);
    screen2HasActivity = genericEntityDao.reloadEntity(screen2HasActivity);
    screen3RnaiHasActivity = genericEntityDao.reloadEntity(screen3RnaiHasActivity);
    screen4HasNoLibraryScreeningActivity =
        genericEntityDao.reloadEntity(screen4HasNoLibraryScreeningActivity);
    screen5TransferredHasActivity = genericEntityDao.reloadEntity(screen5TransferredHasActivity);
    screen6MinMaxSet = genericEntityDao.reloadEntity(screen6MinMaxSet);

    boolean containsFirst = false,
        containsSecond = false,
        containsThird = false,
        containsFourth = false,
        containsFifth = false,
        containsSixth = false;
    for (Pair<Screen, AdministrativeActivity> result : adjustment.screensAdjusted) {
      log.info("entry: " + result.getSecond());
      if (result.getFirst().equals(screen1NoActivities)) containsFirst = true;
      if (result.getFirst().equals(screen2HasActivity)) containsSecond = true;
      if (result.getFirst().equals(screen3RnaiHasActivity)) containsThird = true;
      if (result.getFirst().equals(screen4HasNoLibraryScreeningActivity)) containsFourth = true;
      if (result.getFirst().equals(screen5TransferredHasActivity)) containsFifth = true;
      if (result.getFirst().equals(screen6MinMaxSet)) containsSixth = true;
    }

    // TODO: update test for this
    assertTrue(
        "no screens should not be allowed", adjustment.screenPrivacyAdjustmentNotAllowed.isEmpty());
    assertFalse(
        "the sixth screen should adjusted to allowed",
        adjustment.screensAdjustedToAllowed.isEmpty());

    assertFalse("screen1NoActivities should not have been adjusted", containsFirst);
    assertTrue("screen2HasActivity should have been adjusted", containsSecond);
    assertFalse("screen3RnaiHasActivity should not have been adjusted", containsThird);
    assertFalse(
        "screen4HasNoLibraryScreeningActivity should not have been adjusted", containsFourth);
    assertFalse("screen5TransferredHasActivity should not have been adjusted", containsFifth);
    assertFalse("screen6MinMaxSet should not have been adjusted", containsSixth);
    assertEquals(
        "new screen2HasActivity dataPrivacyExpirationDate() wrong: ",
        newActivityDate.plusDays(ageToExpireFromActivityDateInDays),
        screen2HasActivity.getDataPrivacyExpirationDate());
  }