0 Replies Latest reply on Jul 19, 2007 3:42 PM by Miloslav Vlach

    Why is called @Factory  method always ?

    Miloslav Vlach Novice

      I have a component which display some list and I am using @Factory method. But when I call add, from this component, which do not access to the list, the @Factory method is called. Why ?

      package cz.bpsolutions.ims.services.diary;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.Calendar;
      import java.util.Date;
      import java.util.List;
      import java.util.Locale;
      
      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.PersistenceContextType;
      
      import org.hibernate.Criteria;
      import org.hibernate.criterion.Order;
      import org.hibernate.criterion.Restrictions;
      import org.hibernate.ejb.EntityManagerImpl;
      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Begin;
      import org.jboss.seam.annotations.Destroy;
      import org.jboss.seam.annotations.End;
      import org.jboss.seam.annotations.Factory;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.annotations.datamodel.DataModel;
      import org.jboss.seam.annotations.datamodel.DataModelSelection;
      import org.jboss.seam.annotations.security.Restrict;
      import org.jboss.seam.log.Log;
      
      import cz.bpsolutions.ims.model.Account;
      import cz.bpsolutions.ims.model.DiaryEntry;
      import cz.bpsolutions.ims.model.Request;
      import cz.bpsolutions.ims.services.other.Configuration;
      import cz.bpsolutions.tools.BiInfo;
      
      @Name("diaryEntryAction")
      @Restrict("#{identity.loggedIn}")
      @Stateful
      public class DiaryEntryAction implements Serializable, DiaryEntryActionLocal {
       private static final long serialVersionUID = 6790052898169296206L;
      
       @Logger
       Log log;
      
       Date date;
      
       @In(required = false, scope = ScopeType.SESSION)
       Account loggedUser;
      
       @PersistenceContext(type = PersistenceContextType.EXTENDED)
       EntityManager em;
      
       @DataModel
       List<DiaryEntry> diaryEntries;
      
       @In(required = false)
       DiaryEntry diaryEntry;
      
       @DataModelSelection(value = "diaryEntries")
       DiaryEntry selectedDiaryEntry;
      
       @In(required = false)
       Request request;
      
      
       @Factory("diaryEntries")
       public void load() {
       log.info("loading date #0, user #1", getDate(), loggedUser);
      
       Criteria c = ((EntityManagerImpl) (em.getDelegate())).getSession().createCriteria(DiaryEntry.class);
      
       Date startDay;
       Date endDay;
      
       Calendar cal = Calendar.getInstance(new Locale("cs", "CZ"));
      
       cal.setTime(getDate());
      
       cal.set(Calendar.HOUR, 0);
       cal.set(Calendar.MINUTE, 0);
       cal.set(Calendar.SECOND, 0);
       cal.set(Calendar.AM_PM, 0);
      
       startDay = cal.getTime();
      
       cal.set(Calendar.HOUR, 23);
       cal.set(Calendar.MINUTE, 59);
       cal.set(Calendar.SECOND, 59);
      
       endDay = cal.getTime();
       log.info("dates: #0, #1", startDay, endDay);
      
       c.add(Restrictions.eq("user.id", loggedUser.getId()));
       c.add(Restrictions.or(Restrictions.between("dateFrom", startDay, endDay), Restrictions.between("dateTo", startDay, endDay)));
      
       c.addOrder(Order.asc("dateFrom"));
      
       diaryEntries = c.list();
       log.info("loaded #0 of rows.", diaryEntries.size());
       }
      
       /*
       * (non-Javadoc)
       *
       * @see cz.bpsolutions.ims.services.diary.DiaryEntryActionLocal#save()
       */
      
       public String save() {
       log.info("saving the diary entry #0", diaryEntry);
       loggedUser = em.find(Account.class, loggedUser.getId());
      
       // Date time correction
       Calendar c = Calendar.getInstance(Configuration.locale);
       Calendar c1 = Calendar.getInstance(Configuration.locale);
      
       c.setTime(diaryEntry.getDateFrom());
       c1.setTime(date);
       c.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
       c.set(Calendar.MONTH, c1.get(Calendar.MONTH));
       c.set(Calendar.YEAR, c1.get(Calendar.YEAR));
      
       diaryEntry.setDateFrom(c.getTime());
      
       c.setTime(diaryEntry.getDateTo());
       c.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
       c.set(Calendar.MONTH, c1.get(Calendar.MONTH));
       c.set(Calendar.YEAR, c1.get(Calendar.YEAR));
      
       diaryEntry.setDateTo(c.getTime());
      
       diaryEntry.setUser(loggedUser);
      
       loggedUser.getDiaryEntries().add(diaryEntry);
      
      
       if (request != null && request.getId() != null) {
       request = em.find(Request.class, request.getId());
       diaryEntry.setRequest(request);
       request.getDiaryEntries().add(diaryEntry);
       em.persist(request);
       }
      
      
       em.persist(diaryEntry);
       em.persist(loggedUser);
      
       load();
      
       diaryEntry = new DiaryEntry();
      
       return "saved";
       }
      
       @Begin(join = true)
       public void reset() {
       diaryEntries = null;
       }
      
       public Date getDate() {
       if (date == null)
       date = new Date();
       return date;
       }
      
       @Begin(join = true)
       public void setDate(Date date) {
       this.date = date;
       }
      
       @Begin(join = true)
       public void changeDate() {
       log.info("Changing the date...#0", getDate());
       diaryEntries = null;
       }
      
       @Begin(join = true)
       public void changeDateToNow() {
       log.info("changing to now");
       setDate(new Date());
       diaryEntries = null;
       }
      
       public Long getWorkedHours() {
       Long workedSeconds = 0l;
      
       if (diaryEntries == null) {
       load();
       }
      
       if (diaryEntries != null) {
       for (DiaryEntry d : diaryEntries) {
       if (DiaryType.WORK_DAY.equals(d.getDiaryType())) {
       workedSeconds += d.getDateTo().getTime() - d.getDateFrom().getTime();
       } else if (DiaryType.DOCTOR.equals(d.getDiaryType())) {
       workedSeconds += d.getDateTo().getTime() - d.getDateFrom().getTime();
       } else if (DiaryType.VACATION.equals(d.getDiaryType())) {
       workedSeconds += d.getDateTo().getTime() - d.getDateFrom().getTime();
       } else if (DiaryType.VACATION2.equals(d.getDiaryType())) {
       workedSeconds += d.getDateTo().getTime() - d.getDateFrom().getTime();
       }
       }
       }
      
       log.info("worked seconds: #0", workedSeconds);
       return workedSeconds;
       }
      
       public List<BiInfo> getWorkMapInfo() {
      
       List<BiInfo> ret = new ArrayList<BiInfo>();
      
       int workDay = Configuration.workDay;
       long workedDay = getWorkedHours() / 1000;
       double percent = 0l;
      
       if (diaryEntries != null) {
       Long divider = (workDay > workedDay ? workDay : workedDay);
       if (workDay > workedDay) {
       divider += workDay - workedDay;
       }
       for (DiaryEntry d : diaryEntries) {
       BiInfo b = new BiInfo();
      
       b.first = d.getDiaryType();
       b.second = 100 * (d.getDateTo().getTime() - d.getDateFrom().getTime()) / (1000.0 * divider);
       percent += 100 * (d.getDateTo().getTime() - d.getDateFrom().getTime()) / (1000.0 * divider);
      
       ret.add(b);
       }
      
       if (workDay > workedDay) {
       // neodpracovano
       BiInfo b = new BiInfo();
      
       b.first = DiaryType.NOT_CREATED;
       b.second = 100 - percent;
      
       ret.add(b);
       }
       }
      
       return ret;
       }
      
       @Destroy
       @Remove
       public void remove() {
       }
      
       public String delete() {
       log.info("removind the diaryentry: #0", selectedDiaryEntry);
       em.refresh(selectedDiaryEntry);
      
       Request r = selectedDiaryEntry.getRequest();
       if (r != null) {
       r.getDiaryEntries().remove(selectedDiaryEntry);
      
       em.persist(r);
       }
       em.remove(selectedDiaryEntry);
      
       diaryEntries = null;
       return "deleted";
       }
      
       @End(beforeRedirect=true)
       public String done() {
       log.info("saved...conversation end");
       return "done";
       }
      }
      
      


      21:38:16,359 INFO [DiaryEntryAction] loading date Thu Jul 19 21:38:16 CEST 2007, user cz.bpsolutions.ims.model.Account@1e4605c
      21:38:16,359 INFO [DiaryEntryAction] dates: Thu Jul 19 00:00:00 CEST 2007, Thu Jul 19 23:59:59 CEST 2007
      21:38:16,390 INFO [DiaryEntryAction] loaded 5 of rows.
      21:38:16,390 INFO [DiaryEntryAction] worked seconds: 33420000
      21:38:16,390 INFO [DiaryEntryAction] saving the diary entry cz.bpsolutions.ims.model.DiaryEntry@520467
      21:38:16,406 INFO [DiaryEntryAction] loading date Thu Jul 19 21:38:16 CEST 2007, user cz.bpsolutions.ims.model.Account@141b041
      21:38:16,406 INFO [DiaryEntryAction] dates: Thu Jul 19 00:00:00 CEST 2007, Thu Jul 19 23:59:59 CEST 2007
      21:38:16,406 INFO [DiaryEntryAction] loaded 6 of rows.
      21:38:16,468 INFO [UserService] locating the user mvlach
      21:38:16,468 INFO [UserService] locating the user mvlach
      21:38:16,468 INFO [DiaryEntryAction] worked seconds: 33420000
      21:38:16,484 INFO [DiaryEntryAction] worked seconds: 33420000