@Test
  public void sessionProcessedNewsReporter() {
    this.clearDatabase();
    User u = TeztBeanSimpleFactory.getAdmin();
    u.insert();
    NewsSource nr = new NewsSourceForSessionProcessed();
    assertNotNull(nr);
    List<NewsItem> nis = nr.getNews(SimpleTimeFrame.getToday(), u);
    int nis_size_before = nis.size();

    // insert a processed session
    TimeBooking booking = TeztBeanSimpleFactory.getNewValidBooking();
    booking.getSessionTimeFrame().add(Calendar.YEAR, 1);
    booking.setBooked();
    booking.setUsername(u.getUsername());
    booking.insert();
    booking.processSession();
    Job document = new Job();
    document.setJobId(booking.getId());
    document.setUsername(u.getUsername());
    document.setStep(2);
    document.setIdJobDataProcessing("foo");
    document.setJobSurvey(new JSONObject());
    document.insertOrUpdate();

    // the test
    nis = nr.getNews(SimpleTimeFrame.getToday(), u);
    assertEquals(nis_size_before + 1, nis.size());
  }
 private LogbookEntry getEntryForUserNew(User newUser) {
   LogbookEntry result = this.getNewBaseEntry();
   result.setHeadline("New user: "******"%s was inserted with the role %s", newUser.getFullName(), newUser.getRoleLabel()));
   return result;
 }
 /**
  * construct it with given user for given facility. - status is set to booked, if user has right
  * for direct booking. otherwise to applied. - capacity units of booking is set to min bookable.
  *
  * @see BookingStatus#STATUS_UNSET
  * @see BookingRule#getMinBookableCapacityUnits()
  * @see FamAuth#DIRECT_BOOKING
  * @param user
  * @param facility
  */
 public QueueBooking(User user, FacilityBookable facility) {
   this.setUsername(user.getUsername());
   this.setFacilityKey(facility.getKey());
   if (user.hasRight(FamAuth.DIRECT_BOOKING, facility)) {
     this.setBooked();
   } else {
     this.setApplied();
   }
   this.setBookingRule(facility.getBookingRule());
   this.setCapacityUnits(facility.getBookingRule().getMinBookableCapacityUnits(user));
 }
 public List<SoaActivationPageDocument> getSoasForUser(User user) {
   // TODO #13 do not use FamCouchDBDao#getAll
   List<SoaActivationDocument> soas = this.getAllSoaActivation();
   List<SoaActivationPageDocument> result = null;
   for (SoaActivationDocument soa : soas) {
     if (soa.isActive() && soa.getRoleId().equals(user.getRoleId())) {
       result = soa.getSoaActivePages();
       break;
     }
   }
   return result;
 }
 /** {@inheritDoc} */
 @Override
 public void update(Observable userDao, Object object) {
   LogbookEntry newEntry = null;
   if (this.is(object, User.class)) {
     User user = (User) object;
     if (user.hasBeenAnonymized()) {
       newEntry = this.getEntryForUserAnonymized(user.getUsernameBeforeAnonym());
     } else if (user.hasJustBeenInserted()) {
       // a new user was inserted
       newEntry = this.getEntryForUserNew(user);
     } else if (user.hasJustBeenDeleted()) {
       newEntry = this.getEntryForUserDeleted(user);
     } else {
       User old = FamDaoProxy.userDao().getUserFromUsername(user.getUsername());
       newEntry = this.getEntryForUserUpdate(old, user);
     }
   } else if (this.is(object, ContactDetail.class)) {
     ContactDetail cd = (ContactDetail) object;
     newEntry = this.getEntryForContactDetails(cd);
   }
   if (newEntry != null) {
     newEntry.insert();
   } else {
     FamLog.error("observed an unknown event: " + object.getClass(), 201403061006l);
   }
 }
 private LogbookEntry getEntryForUserUpdate(User oldUser, User newUser) {
   LogbookEntry result = this.getNewBaseEntry();
   result.setHeadline("Update user: "******"please report error 201402061143");
   }
   result.setContent(StringUtils.join(messages.toArray(), "\r\n")); // FIXME check delimiter
   return result;
 }
 /** {@inheritDoc}} */
 @Override
 public List<TermsOfUsePage> getActiveTermsOfUsePages(User user) {
   List<TermsOfUsePage> result = new ArrayList<TermsOfUsePage>();
   ViewResult<TermsOfUsePage> pages =
       FamCouchDBDao.database()
           .query(
               "/_design/as/_view/terms_of_use_pages_active",
               TermsOfUsePage.class,
               null,
               null,
               null);
   for (ValueRow<TermsOfUsePage> page : pages.getRows()) {
     if (page.getValue().getRoleId().equals(user.getRoleId())) {
       result.add(page.getValue());
     }
   }
   return result;
 }
  @Test
  public void storeAndGetJobAttachment() throws IOException {
    this.clearDatabase();
    User admin = TeztBeanSimpleFactory.getAdmin();

    File testFileOriginal =
        new File(
            System.getProperty("user.dir")
                + File.separator
                + ".."
                + File.separator
                + "fam-web"
                + File.separator
                + "src"
                + File.separator
                + "main"
                + File.separator
                + "webapp"
                + File.separator
                + "demo"
                + File.separator
                + "invoice_demo_nursery_school.pdf");
    assertTrue(testFileOriginal.canRead());

    File directory =
        new File(
            FamConnector.fileExchangeDir()
                + File.separator
                + "users"
                + File.separator
                + admin.getUsername());
    if (!directory.exists()) {
      directory.mkdir();
      directory.setWritable(true);
    }
    assertTrue(directory.canWrite());

    File testFile = new File(directory.getAbsolutePath() + File.separator + "test.pdf");
    if (testFile.exists() == false
        || testFile.canRead() == false
        || testFile.length() != testFileOriginal.length()) {
      InputStream in = new FileInputStream(testFileOriginal);
      OutputStream out = new FileOutputStream(testFile);

      byte[] buf = new byte[1024];
      int len;
      while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
      in.close();
      out.close();
    }

    assertEquals(testFileOriginal.length(), testFile.length());

    TimeBooking booking = TeztBeanSimpleFactory.getNewValidBooking();
    booking.setBooked();
    booking.insert();

    Job document = new Job();
    document.setJobId(booking.getId());
    document.setUsername(admin.getUsername());
    document.setStep(0);
    document.setIdJobDataProcessing("foo");
    document.setJobSurvey(new JSONObject());

    List<File> attachments = new ArrayList<File>(1);
    attachments.add(testFile);
    document.addAttachments(attachments);
    assertTrue(document.insertOrUpdate());

    // get it back
    byte[] got_back =
        FamCouchDBDao.getInstance().getAttachment(document.getId(), testFile.getName());
    assertEquals(got_back.length, testFile.length());
    assertEquals(got_back.length, testFileOriginal.length());

    Byte[] testBs = new File2ByteArray().process(testFile);
    Byte[] testBs2 = new File2ByteArray().process(testFileOriginal);
    for (int i = 0; i < testBs.length; i++) {
      assertEquals(testBs[i].byteValue(), got_back[i]);
      assertEquals(testBs2[i].byteValue(), got_back[i]);
    }
  }
 private LogbookEntry getEntryForUserDeleted(User user) {
   LogbookEntry result = this.getNewBaseEntry();
   result.setHeadline("Deleted user: "******"%s was deleted", user.getFullName()));
   return result;
 }