Example #1
0
  private void processDefaultHolder(
      Entity entity, AnnotationMetadata<DefaultHolder> defaultHolder) {

    Query query = ContextUtil.getEntityFactory().buildQuery(entity.getEntityType());
    for (String filter : defaultHolder.getAnnotation().filters()) {
      query.addCriterion(
          new Path(filter),
          Condition.EQ,
          ContextUtil.getMRS()
              .getPropertyMetadata(entity.getEntityType(), filter)
              .getValue(entity));
    }
    List<Entity> existingEntities = ContextUtil.getDRS().findByQuery(query);
    PropertyMetadata propertyMetadata =
        ContextUtil.getMRS()
            .getPropertyMetadata(entity.getEntityType(), defaultHolder.getField().getName());
    boolean currentValue = (Boolean) propertyMetadata.getValue(entity);

    if (existingEntities.size() > 1 && currentValue) {
      for (Entity existingEntity : existingEntities) {
        if (!existingEntity.equals(entity) && (Boolean) propertyMetadata.getValue(existingEntity)) {
          propertyMetadata.setValue(existingEntity, false);
          sessionFactory.getCurrentSession().saveOrUpdate(existingEntity);
        }
      }
    } else if (existingEntities.size() == 1 && !currentValue) {
      propertyMetadata.setValue(entity, true);
      sessionFactory.getCurrentSession().saveOrUpdate(entity);
    }
  }
Example #2
0
  public <E extends Entity> void save(E entity) {
    sessionFactory.getCurrentSession().saveOrUpdate(entity);

    for (AnnotationMetadata<DefaultHolder> defaultHolder :
        ContextUtil.getMRS().getAnnotationMetadata(entity.getEntityType(), DefaultHolder.class)) {
      processDefaultHolder(entity, defaultHolder);
    }
  }