2 Replies Latest reply on Jul 13, 2010 4:23 AM by deadlock gr

    Is there a @Create in components.xml?

    deadlock gr Newbie

      Is there a way to declare an initialization method in components.xml?


      Like a @Create?

        • 1. Re: Is there a @Create in components.xml?
          Herberson Silva Newbie

          Define a Seam component with Application or Session scope and define a method annotated with @Create, this tells to Seam to execute that method on component initialization/instalation.


          This example initializes the application admin user.



          package br.com.herberson.example.session;
          
          import java.io.Serializable;
          import java.util.ArrayList;
          import java.util.Date;
          import java.util.List;
          import java.util.Random;
          
          import javax.persistence.EntityManager;
          import javax.persistence.NoResultException;
          
          import org.apache.commons.beanutils.BeanUtils;
          import org.jboss.seam.ScopeType;
          import org.jboss.seam.annotations.Create;
          import org.jboss.seam.annotations.In;
          import org.jboss.seam.annotations.Logger;
          import org.jboss.seam.annotations.Name;
          import org.jboss.seam.annotations.Scope;
          import org.jboss.seam.annotations.Startup;
          import org.jboss.seam.annotations.Transactional;
          import org.jboss.seam.log.Log;
          
          import br.com.herberson.example.entity.Company;
          import br.com.herberson.example.entity.Role;
          import br.com.herberson.example.entity.User;
          
          @Name("firststweard.InitializeApp")
          @Scope(ScopeType.APPLICATION)
          @Startup(depends="entityManager")
          public class InitializeApp implements Serializable {
               // default serial version
               private static final long serialVersionUID = 1L;
               
               @Logger
               private Log logger; // NOPMD by herberson on 06/07/10 15:04
               
               @In
               private EntityManager entityManager; // NOPMD by herberson on 06/07/10 15:04
               
               @Create
               @Transactional()
               public void init() {
                    logger.info("Initilizing application...");
                    initAdminUser();
               } //end method
               
               public void initAdminUser() {
                    logger.info("Call initAdminUser():void...");
                    
                    try {
                         entityManager.createNamedQuery(User.FIND_BY_USER_NAME).setParameter("userName", "admin").getSingleResult(); // NOPMD by herberson on 06/07/10 15:05
                    } catch (NoResultException nre) {
                         dumpAdmin(true, MAX_ROLES, MAX_COMPANIES);
                         logger.info("Admin user initialized...");
                    } //end try ... catch
               } //end method
               
               private void dumpAdmin(boolean markLastLogin) {
                    User admin;
                    
                    admin = new User();
                    admin.setFirstName("System");
                    admin.setLastName("Administrator");
                    admin.setUserName("admin");
                    admin.setPassword("123456");
                    admin.setActive(true);
                    admin.setRoles(new ArrayList<Role>());
                    admin.setCompanies(new ArrayList<Company>());
                    admin.getRoles().add(entityManager.find(Role.class, 1));
                    admin.getRoles().add(entityManager.find(Role.class, 2));
                    admin.getRoles().add(entityManager.find(Role.class, 3));
                    if (markLastLogin) {
                         admin.setLastSuccessFullLogin(new java.util.Date());
                    } else {
                         admin.setLastSuccessFullLogin(null);
                    }
                    entityManager.persist(admin);
                    entityManager.flush();
               } //end method
               
          } //end class