Got feedback or spotted a mistake?

Leave a comment at the end of this page or email contact@krishagni.com

An Example Print Token based on Annotation Field

//
// Output specimen destruction date that is captured in "SpecimenCustomDetails"
// annotation form attached at the Specimen level
//
public class SpecimenDestructionDatePrintToken extends AbstractLabelTmplToken implements LabelTmplToken {
   private FormDao formDao;

   //
   // setter to inject formDao bean defined in core app context
   // 
   public void setFormDao(FormDao formDao) {
      this.formDao = formDao;
   }

   @Override
   public String getName() {
      //
      // The name of our annotation field value token
      //
      return "specimen_destruction_date";
   }

   @Override
   public String getReplacement(Object object) {
      Specimen spmn = (Specimen) object;

      //
      // Step 1: Retrieve the form by name (SpecimenCustomDetails)
      //
      Container form = Container.getContainer("SpecimenCustomDetails");
      if (form == null) {
         //
         // Return an empty string when there is no form by name "SpecimenCustomDetails"
         //
         return StringUtils.EMPTY;
      }

      //
      // Step 2: Retrieve all "SpecimenCustomDetails" records metadata for the input specimen
      //
      Map<Long, List<FormRecordSummary>> records = formDao.getFormRecords(spmn.getId(), "Specimen", form.getId());
      if (records.isEmpty()) {
         //
         // Return an empty string when no records are saved for the specimen
         //
         return StringUtils.EMPTY;
      }

      //
      // Step 3: Sort form records based on their order of creation and
      // get the first record ID (records metadata)
      //
      FormRecordSummary record = records.get(form.getId()).stream()
         .sorted((r1, r2) -> r1.getRecordId().compareTo(r2.getRecordId()))
         .findFirst().get();


      //
      // Step 4: Get the form record data
      //
      FormDataManager mgr = new FormDataManagerImpl(false);
      FormData formData = mgr.getFormData(form, record.getRecordId());

      //
      // Step 5: Retrieve the field value that we are interested in
      // i.e. destructionDate
      //
      ControlValue value = formData.getFieldValue("destructionDate");

      //
      // Step 6: Get time in milliseconds since Epoch and convert it
      // to a date string conforming to the format specified in the locale
      //
      String timeInStr = value.toString();
      return Utility.getDateString(new Date(Long.parseLong(timeInStr)));
   }
}


The details of how to package above token implementation in plugin and register with print tokens registrar can be found in this document.

Got feedback or spotted a mistake?

Leave a comment at the end of this page or email contact@krishagni.com