You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.7 KiB
Java

package cokr.xit.foundation.component;
import java.util.Date;
import javax.annotation.Resource;
import org.egovframe.rte.fdl.property.EgovPropertyService;
import org.springframework.context.MessageSource;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.Access;
import cokr.xit.foundation.Log;
import cokr.xit.foundation.UserInfo;
import cokr.xit.foundation.util.DateFormats;
/**spring framework가 관리하는 컴포넌트를 정의할 때 상속받는 클래스.
* <p>AbstractBean를 상속받는 클래스는
* <ul><li>{@link #currentAccess() 현재 접속 정보}</li>
* <li>{@link #currentUser() 현재 접속한 사용자 정보}</li>
* <li>{@link #properties 프로퍼티 서비스}</li>
* <li>{@link #message(String, String...) 메시지 처리}</li>
* </ul>
* 등의 기능을 사용할 수 있다.
* <p>AbstractBean의 상속 클래스는 '업무 용어 또는 기능 이름' + 'Bean'과 같이 이름을 명시한다.<br />
* 그리고 정의한 클래스를 애플리케이션 컨텍스트에 등록하려면, @Component 어노테이션을 적용하고 카멜 표기법으로 이름을 지정한다.<br />
* 다음은 그 예이다.
* <pre> package cokr.xit.example.business.service.bean;
* {@code @Component("businessBean")}
* public class BusinessBean extends AbstractBean {
* ...
* }</pre>
* <p>정의한 컴포넌트를 사용하려면 @Resource 어노테이션에 이름을 명시해 객체에 주입한다.
* <pre><code> @Resource(name = "businessBean")
* BusinessBean businessBean;</code></pre>
*/
public abstract class AbstractBean extends AbstractComponent {
/** 프로퍼티 서비스 */
@Resource(name="propertyService")
protected EgovPropertyService properties;
/** 메시지 소스 */
@Resource(name="messageSource")
protected MessageSource messages;
/** 날짜 포맷 모음 */
protected DateFormats dateFormats = new DateFormats();
private String logName;
/**로그이름을 설정한다.
* @param logName 로그이름
*/
public void setLogName(String logName) {
this.logName = logName;
}
/**현재 접근한 사용자를 반환한다.
* @param <T> 사용자 정보 유형
* @return 현재 접근한 사용자
*/
protected <T extends UserInfo> T currentUser() {
return UserInfo.current();
}
/**현재 사용자의 접근정보를 반환한다.
* @return 현재 사용자의 접근정보
*/
protected static Access currentAccess() {
return Access.current();
}
/**메시지 리소스 파일에서 key로 정의된 문자열과 args로 메시지를 만들어 반환한다.
* @param key 메시지 리소스 파일의 문자열 key
* @param args 해당 문자열에 전달할 인자
* @return 메시지
*/
protected String message(String key, String... args) {
return messages.getMessage(key, ifEmpty(args, () -> null), "", currentAccess().getLocale());
}
/**t가 공백이 아닌지 확인하고 t를 반환한다.
* t가 공백이면, NullPointerException을 발생시키며, 메시지는 message-common 프로퍼티 파일의 'valueRequired'키에 설정된 값으로 만든다.
* @param <T> 객체 유형
* @param t 값이나 객체
* @param name assertion 실패 시 오류 메시지에 사용할 이름
* @return t
*/
protected final <T> T required(T t, String name) {
if (!isEmpty(t)) return t;
throw new NullPointerException(message("valueRequired", name));
}
@Override
protected Log log() {
return isEmpty(logName) ? super.log() : Log.get(logName);
}
/**주어진 Date에 지정한 날짜수만큼의 차이의 Date를 반환한다.
* @param date 날짜
* @param days 날짜수
* @return 새 Date
*/
protected static Date dateDiff(Date date, int days) {
return new Date(date.getTime() + (1000L * 60L * 60L * 24L * days));
}
}