@Springfield 는 HTTP Session Context Attribute 에 접근하기 위해 com.u2ware.springfield.support.context. ContextBroker를 제공한다.
package com.u2ware.springfield.support.context; public interface ContextBroker { public <O> void put(O object); public <O> O get(Class<O> type); public <O> O get(Class<O> type, boolean throwException); public <O> O remove(Class<O> type); }
package com.u2ware.springfield.sample.others.context; import javax.persistence.Entity; import javax.persistence.Id; import javax.validation.constraints.NotNull; import lombok.Getter; import lombok.Setter; import org.hibernate.annotations.Type; import org.joda.time.DateTime; import org.springframework.format.annotation.DateTimeFormat; import com.u2ware.springfield.config.Springfield; import com.u2ware.springfield.config.Springfield.Strategy; @Springfield( strategy=Strategy.JPA ) @Entity public class DayStep { @Id @Getter @Setter private @NotNull String name; @Type(type="org.joda.time.contrib.hibernate.PersistentDateTime") @Getter @Setter private @NotNull @DateTimeFormat(pattern="yyyy-MM-dd") DateTime step; }
package com.u2ware.springfield.sample.others.context; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.u2ware.springfield.repository.EntityRepository; import com.u2ware.springfield.service.EntityServiceImpl; import com.u2ware.springfield.support.context.ContextBroker; @Service public class DayStepService extends EntityServiceImpl<DayStep, DayStep>{ @Autowired @Qualifier("sessionContextBroker") private ContextBroker sessionContextBroker; @Autowired public DayStepService( @Qualifier("dayStepRepository")EntityRepository<DayStep, ?> r) { super("dayStepRepository", r); } @Override @Transactional public Object createForm(DayStep entity) { DayStep saved = sessionContextBroker.get(DayStep.class, false); if(saved != null){ entity.setName(saved.getName()); entity.setStep(saved.getStep().plusDays(1)); } return entity; } @Override @Transactional public Object create(DayStep entity) { DayStep newEntity = getRepository().create(entity); sessionContextBroker.put(newEntity); return newEntity; } }