te3

te

package xxx.common.validation;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

@Documented
@Constraint(validatedBy = {NotNullBySelectValidator.class})
@Target({TYPE, /* FIELD, */ ANNOTATION_TYPE})
@Retention(RUNTIME)
public @interface NotNullBySelect {
  String message() default "kirin.common.validation.NotNullBySelect.message";

  Class<?>[] groups() default {};

  Class<? extends Payload>[] payload() default {};

  /** FieldBys names */
  String[] fieldBys();

  /** FieldByValues */
  String[] fieldByValues();

  /** FieldTarget names */
  String[] fieldTargets();

  @Target({TYPE, ANNOTATION_TYPE})
  @Retention(RUNTIME)
  @Documented
  public @interface List {
    NotNullBySelect[] value();
  }
}


package xxx.common.validation;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.ObjectUtils;
import jp.co.intra_mart.common.aid.jdk.java.text.NamedMessageFormat;
import jp.co.intra_mart.foundation.security.exception.AccessSecurityException;
import jp.co.intra_mart.foundation.security.message.MessageManager;

public class NotNullBySelectValidator implements ConstraintValidator<NotNullBySelect, Object> {
  // @Inject
  // MessageSource messageSource;


  private String[] fieldBys;

  private String fieldBy;

  private String[] fieldByValues;

  private String[] fieldTargets;

  private String fieldTarget;

  private String message;

  private String messageDsp;

  public void initialize(NotNullBySelect constraintAnnotation) {
    fieldBys = constraintAnnotation.fieldBys();
    fieldByValues = constraintAnnotation.fieldByValues();
    fieldTargets = constraintAnnotation.fieldTargets();
    message = constraintAnnotation.message();
    try {
      messageDsp = MessageManager.getInstance().getMessage(message);
    } catch (AccessSecurityException e) {
      // 処理なし
    }
  }

  public boolean isValid(Object value, ConstraintValidatorContext context) {
    if (value == null) {
      return false;
    }
    BeanWrapper beanWrapper = new BeanWrapperImpl(value);
    AtomicInteger i = new AtomicInteger(0);
    Stream.of(fieldBys).forEach(f -> {
      fieldBy = f;
      Object fieldByVal = beanWrapper.getPropertyValue(f);
      String fieldByProp = "";
      String fieldTargetProp = "";
      if (!ObjectUtils.isEmpty(fieldByVal)
          && ObjectUtils.nullSafeEquals(fieldByVal, fieldByValues[i.get()])) {
        context.disableDefaultConstraintViolation();
        fieldTarget = fieldTargets[i.getAndIncrement()];
        try {
          fieldByProp = MessageManager.getInstance().getMessage(fieldBy);
          fieldTargetProp = MessageManager.getInstance().getMessage(fieldTarget);
        } catch (AccessSecurityException e) {
          // 処理なし
        }

        Map<String, String> params = new HashMap<>();
        params.put("fieldBy", fieldByProp);
        params.put("fieldTarget", fieldTargetProp);
        String msg = NamedMessageFormat.format(messageDsp, params);

        context.buildConstraintViolationWithTemplate(msg).addPropertyNode(fieldTarget)
            .addConstraintViolation();
      }
    });

    return (i.get() == 0);
  }
}



@Documented
@Constraint(validatedBy = {ConfirmValidator.class})
@Target({TYPE, ANNOTATION_TYPE}) // (1)
@Retention(RUNTIME)
public @interface Confirm {

  /**
   * メッセージ定義
   * 
   * @return
   */
  String message() default "{kirin.common.validation.Confirm.message}";

  /**
   * groups定義
   * 
   * @return
   */
  Class<?>[] groups() default {};

  /**
   * payload定義
   * 
   * @return
   */
  Class<? extends Payload>[] payload() default {};

  /** Field name. */
  String field(); // (2)

  @Target({TYPE, ANNOTATION_TYPE})
  @Retention(RUNTIME)
  @Documented
  public @interface List {
    /**
     * value定義
     * 
     * @return
     */
    Confirm[] value();
  }
}

public class ConfirmValidator implements ConstraintValidator<Confirm, Object> {

  private String field;

  private String confirmField;

  private String message;

  /*
   * (非 Javadoc)
   * 
   * @see javax.validation.ConstraintValidator#initialize(java.lang.annotation.Annotation)
   */
  /**
   * 初期化
   */
  public void initialize(Confirm constraintAnnotation) {
    field = constraintAnnotation.field();
    confirmField = "confirm" + StringUtils.capitalize(field);
    message = constraintAnnotation.message();
  }

  /*
   * (非 Javadoc)
   * 
   * @see javax.validation.ConstraintValidator#isValid(java.lang.Object,
   * javax.validation.ConstraintValidatorContext)
   */
  /**
   * 有効か判定
   */
  public boolean isValid(Object value, ConstraintValidatorContext context) {
    BeanWrapper beanWrapper = new BeanWrapperImpl(value);
    Object fieldValue = beanWrapper.getPropertyValue(field);
    Object confirmFieldValue = beanWrapper.getPropertyValue(confirmField);
    boolean matched = ObjectUtils.nullSafeEquals(fieldValue, confirmFieldValue);
    if (matched) {
      return true;
    } else {
      context.disableDefaultConstraintViolation();
      context.buildConstraintViolationWithTemplate(message).addPropertyNode(field)
          .addConstraintViolation();
      return false;
    }
  }
}
@Confirm(field = "password")
// @NotNullBySelect(fieldBys = { "selectReportKbn" }, fieldByValues = { "1" },
// fieldTargets = { "textArea" })
@NotNullBySelect(fieldBys = {"selectReportKbn", "checkReportKbn"}, fieldByValues = {"1", "2"},
    fieldTargets = {"textArea", "note"})


@Component("xxx.common.component.DownloadView")
public class DownloadView extends AbstractFileDownloadView {

  @Override
  protected void addResponseHeader(final Map<String, Object> model,
      final HttpServletRequest request, final HttpServletResponse response) {

    // ダウンロードファイル用のヘッダを設定する。
    final String disposition;
    try {
      // サーバのcharacter encodingを第二引数に設定してください。
      final Storage<?> storage = (Storage<?>) model.get("storage");
      disposition = ResponseUtil.encodeFileName(request, "UTF-8", storage.getName());
    } catch (final UnsupportedEncodingException e) {
      throw new SystemException("download view error code", e);
    }
    response.setHeader("Content-Disposition", "attachment;" + disposition);
    response.setContentType("application/octet-stream");
  }

  @Override
  protected InputStream getInputStream(final Map<String, Object> model,
      final HttpServletRequest request) throws IOException {

    // modelに設定した storage を取得し、InputStreamを返す。
    final Storage<?> storage = (Storage<?>) model.get("storage");
    return storage.open();
  }