Package org.openbravo.base.session

Examples of org.openbravo.base.session.SessionFactoryController


    // Caching model (tables, table-references, search-references,
    // list-references)
    // Changed to use the SessionHandler directly because the dal
    // layer uses the ModelProvider, so otherwise there will be a
    // cyclic relation.
    final SessionFactoryController sessionFactoryController = new ModelSessionFactoryController();
    final Session session = sessionFactoryController.getSessionFactory().openSession();
    final Transaction tx = session.beginTransaction();
    try {
      log.debug("Read model from db");
      tables = list(session, Table.class);

      // read the columns in one query and assign them to the table
      final List<Column> cols = readColumns(session);
      assignColumnsToTable(cols);

      refTable = list(session, RefTable.class);
      refSearch = list(session, RefSearch.class);
      refList = list(session, RefList.class);
      modules = retrieveModules(session);
      tables = removeInvalidTables(tables);

      for (final RefTable rt : refTable) {
        refTableMap.put(rt.getId(), rt);
      }
      for (final RefSearch rs : refSearch) {
        // note mapped by reference id
        refSearchMap.put(rs.getReference(), rs);
      }

      // this map stores the mapped tables
      tablesByTableName = new HashMap<String, Table>();
      for (final Table t : tables) {
        if (t.isView()) {
          continue;
        }
        // tables are stored case insensitive!
        tablesByTableName.put(t.getTableName().toLowerCase(), t);
      }

      log.debug("Setting referencetypes for columns");
      for (final Table t : tablesByTableName.values()) {
        t.setReferenceTypes(ModelProvider.instance);
      }

      log.debug("Setting List Values for columns");
      for (final RefList rl : refList) {
        rl.setAllowedValue();
      }

      model = new ArrayList<Entity>();
      entitiesByName = new HashMap<String, Entity>();
      entitiesByClassName = new HashMap<String, Entity>();
      entitiesByTableName = new HashMap<String, Entity>();
      entitiesByTableId = new HashMap<String, Entity>();
      entitiesWithTreeType = new ArrayList<Entity>();
      for (final Table t : tablesByTableName.values()) {
        log.debug("Building model for table " + t.getTableName());
        final Entity e = new Entity();
        e.initialize(t);
        model.add(e);
        entitiesByClassName.put(e.getClassName(), e);
        entitiesByName.put(e.getName(), e);
        entitiesByTableName.put(t.getTableName().toUpperCase(), e);
        entitiesByTableId.put(t.getId(), e);
        if (e.getTreeType() != null) {
          entitiesWithTreeType.add(e);
        }
      }

      // in the second pass set all the referenceProperties
      // and targetEntities
      // uses global member tablesByTableName
      setReferenceProperties();

      // add virtual property for the case that the
      // id property is also a reference (a foreign key)
      // In this case hibernate requires two mappings
      // one for the id (a string) and for the reference
      // in addition the id generation strategy should be set
      // to foreign.
      log.debug("Setting virtual property for many-to-one id's");
      setVirtualPropertiesForReferenceId();

      buildUniqueConstraints(session, sessionFactoryController);

      final Map<String, Boolean> colMandatories = getColumnMandatories(session,
          sessionFactoryController);

      // initialize the name and also set the mandatory value on the basis
      // of the real not-null in the database!
      for (final Entity e : model) {
        for (final Property p : e.getProperties()) {
          p.initializeName();
          if (p.getColumnName() != null) {
            final Boolean mandatory = colMandatories.get(createColumnMandatoryKey(e.getTableName(),
                p.getColumnName()));
            if (mandatory != null) {
              p.setMandatory(mandatory);
            } else {
              log.warn("Column " + p + " mandatory is null");
            }
          }
        }

        // dumpPropertyNames(e);
      }
    } finally {
      log.debug("Closing session and sessionfactory used during model read");
      tx.commit();
      session.close();
      sessionFactoryController.getSessionFactory().close();
    }
    clearLists();
  }
View Full Code Here


   * Returns the tables in the database, is usefull for debugging purposes.
   *
   * @return list of tables in the database
   */
  public List<Table> getTables() {
    final SessionFactoryController sessionFactoryController = new ModelSessionFactoryController();
    final Session session = sessionFactoryController.getSessionFactory().openSession();
    final Transaction tx = session.beginTransaction();
    try {
      tables = list(session, Table.class);
      // read the columns in one query and assign them to the table
      final List<Column> cols = readColumns(session);
      assignColumnsToTable(cols);
      return tables;
    } finally {
      log.debug("Closing session and sessionfactory used during model read");
      tx.commit();
      session.close();
      sessionFactoryController.getSessionFactory().close();
    }
  }
View Full Code Here

   * @return the last time that one of the relevant Application Dictionary objects was modified.
   *         Relevant AD objects are: Table, Column, Reference, RefList, RefSearch, RefTable,
   *         Module, Package.
   */
  public long computeLastUpdateModelTime() {
    final SessionFactoryController sessionFactoryController = new ModelSessionFactoryController();
    final Session session = sessionFactoryController.getSessionFactory().openSession();
    final Transaction tx = session.beginTransaction();
    try {
      // compute the last updated time
      long currentLastTimeUpdated = 0;
      currentLastTimeUpdated = getLastUpdated(Table.class, currentLastTimeUpdated, session);
      currentLastTimeUpdated = getLastUpdated(Column.class, currentLastTimeUpdated, session);
      currentLastTimeUpdated = getLastUpdated(RefTable.class, currentLastTimeUpdated, session);
      currentLastTimeUpdated = getLastUpdated(RefSearch.class, currentLastTimeUpdated, session);
      currentLastTimeUpdated = getLastUpdated(RefList.class, currentLastTimeUpdated, session);
      currentLastTimeUpdated = getLastUpdated(Module.class, currentLastTimeUpdated, session);
      currentLastTimeUpdated = getLastUpdated(Package.class, currentLastTimeUpdated, session);
      currentLastTimeUpdated = getLastUpdated(Reference.class, currentLastTimeUpdated, session);
      return currentLastTimeUpdated;
    } finally {
      tx.commit();
      session.close();
      sessionFactoryController.getSessionFactory().close();
    }
  }
View Full Code Here

   * concept.
   */
  public void testHiddenUpdates() {
    setUserContext("0");

    final SessionFactoryController currentSFC = SessionFactoryController.getInstance();
    try {
      final SessionFactoryController newSFC = new LocalSessionFactoryController();
      SessionFactoryController.setInstance(newSFC);
      SessionFactoryController.getInstance().reInitialize();
      SessionHandler.getInstance().commitAndClose();

      // System.err.println(SessionFactoryController.getInstance().
View Full Code Here

TOP

Related Classes of org.openbravo.base.session.SessionFactoryController

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.