Ejemplo n.º 1
0
  public String[] getEntityNames() {
    Iterator<Entry<String, CFCInfo>> it = cfcs.entrySet().iterator();
    String[] names = new String[cfcs.size()];
    int index = 0;
    while (it.hasNext()) {
      names[index++] = HibernateCaster.getEntityName(it.next().getValue().getCFC());
      // names[index++]=it.next().getValue().getCFC().getName();
    }
    return names;

    // return cfcs.keySet().toArray(new String[cfcs.size()]);
  }
Ejemplo n.º 2
0
  public void createMapping(
      PageContext pc, Component cfc, DatasourceConnection dc, ORMConfiguration ormConf)
      throws PageException {
    ComponentPro cfcp = ComponentUtil.toComponentPro(cfc);
    String id = id(HibernateCaster.getEntityName(cfcp));
    CFCInfo info = cfcs.get(id);
    // Long modified=cfcs.get(id);
    String xml;
    long cfcCompTime = ComponentUtil.getCompileTime(pc, cfcp.getPageSource());
    if (info == null
        || (ORMUtil.equals(info.getCFC(), cfcp))) { // && info.getModified()!=cfcCompTime
      StringBuilder sb = new StringBuilder();

      long xmlLastMod = loadMapping(sb, ormConf, cfcp);
      Element root;
      // create maaping
      if (true || xmlLastMod < cfcCompTime) { // MUSTMUST
        configuration = null;
        Document doc = null;
        try {
          doc = XMLUtil.newDocument();
        } catch (Throwable t) {
          t.printStackTrace();
        }

        root = doc.createElement("hibernate-mapping");
        doc.appendChild(root);
        pc.addPageSource(cfcp.getPageSource(), true);
        try {
          HBMCreator.createXMLMapping(pc, dc, cfcp, ormConf, root, this);
        } finally {
          pc.removeLastPageSource(true);
        }
        xml = XMLCaster.toString(root.getChildNodes(), true);
        saveMapping(ormConf, cfcp, root);
      }
      // load
      else {
        xml = sb.toString();
        root = Caster.toXML(xml).getOwnerDocument().getDocumentElement();
        /*print.o("1+++++++++++++++++++++++++++++++++++++++++");
        print.o(xml);
        print.o("2+++++++++++++++++++++++++++++++++++++++++");
        print.o(root);
        print.o("3+++++++++++++++++++++++++++++++++++++++++");*/

      }
      cfcs.put(id, new CFCInfo(ComponentUtil.getCompileTime(pc, cfcp.getPageSource()), xml, cfcp));
    }
  }
Ejemplo n.º 3
0
  private boolean isEntity(ComponentPro cfc, String cfcName, String name) {
    if (!StringUtil.isEmpty(cfcName)) {
      if (cfc.equalTo(cfcName)) return true;

      if (cfcName.indexOf('.') != -1) {
        String path = cfcName.replace('.', '/') + ".cfc";
        Resource[] locations = ormConf.getCfcLocations();
        for (int i = 0; i < locations.length; i++) {
          if (locations[i].getRealResource(path).equals(cfc.getPageSource().getFile())) return true;
        }
        return false;
      }
    }

    if (cfc.equalTo(name)) return true;
    return name.equalsIgnoreCase(HibernateCaster.getEntityName(cfc));
  }
Ejemplo n.º 4
0
  public Component getEntityByEntityName(String entityName, boolean unique) throws PageException {
    ComponentPro cfc;

    CFCInfo info = cfcs.get(entityName.toLowerCase());
    if (info != null) {
      cfc = info.getCFC();
      return unique ? (Component) cfc.duplicate(false) : cfc;
    }

    if (arr != null) {
      Iterator<ComponentPro> it2 = arr.iterator();
      while (it2.hasNext()) {
        cfc = it2.next();
        if (HibernateCaster.getEntityName(cfc).equalsIgnoreCase(entityName))
          return unique ? (Component) cfc.duplicate(false) : cfc;
      }
    }

    throw new ORMException(this, "entity [" + entityName + "] does not exist");
  }
Ejemplo n.º 5
0
  private synchronized SessionFactory getSessionFactory(PageContext pc, boolean init)
      throws PageException {
    ApplicationContextPro appContext = ((ApplicationContextPro) pc.getApplicationContext());
    if (!appContext.isORMEnabled())
      throw new ORMException(this, "ORM is not enabled in application.cfc/cfapplication");

    this.hash = hash(appContext);

    // datasource
    String dsn = appContext.getORMDatasource();
    if (StringUtil.isEmpty(dsn))
      throw new ORMException(this, "missing datasource defintion in application.cfc/cfapplication");
    if (!dsn.equalsIgnoreCase(datasource)) {
      configuration = null;
      if (_factory != null) _factory.close();
      _factory = null;
      datasource = dsn.toLowerCase();
    }

    // config
    ormConf = appContext.getORMConfiguration();

    // List<Component> arr = null;
    arr = null;
    if (init) {
      if (ormConf.autogenmap()) {
        arr = HibernateSessionFactory.loadComponents(pc, this, ormConf);
        cfcs.clear();
      } else
        throw new HibernateException(this, "orm setting autogenmap=false is not supported yet");
    }

    // load entities
    if (!ArrayUtil.isEmpty(arr)) {
      loadNamingStrategy(ormConf);

      DatasourceConnectionPool pool =
          ((ConfigWebImpl) pc.getConfig()).getDatasourceConnectionPool();
      DatasourceConnection dc =
          pool.getDatasourceConnection(pc, pc.getConfig().getDataSource(dsn), null, null);
      // DataSourceManager manager = pc.getDataSourceManager();
      // DatasourceConnection dc=manager.getConnection(pc,dsn, null, null);
      this.ds = dc.getDatasource();
      try {
        Iterator<ComponentPro> it = arr.iterator();
        while (it.hasNext()) {
          createMapping(pc, it.next(), dc, ormConf);
        }
      } finally {
        pool.releaseDatasourceConnection(dc);
        // manager.releaseConnection(pc,dc);
      }
      if (arr.size() != cfcs.size()) {
        ComponentPro cfc;
        String name, lcName;
        Map<String, String> names = new HashMap<String, String>();
        Iterator<ComponentPro> it = arr.iterator();
        while (it.hasNext()) {
          cfc = it.next();
          name = HibernateCaster.getEntityName(cfc);
          lcName = name.toLowerCase();
          if (names.containsKey(lcName))
            throw new ORMException(
                this,
                "Entity Name ["
                    + name
                    + "] is ambigous, ["
                    + names.get(lcName)
                    + "] and ["
                    + cfc.getPageSource().getDisplayPath()
                    + "] use the same entity name.");
          names.put(lcName, cfc.getPageSource().getDisplayPath());
        }
      }
    }
    arr = null;
    if (configuration != null) return _factory;

    // MUST
    // cacheconfig
    // cacheprovider
    // ...

    String mappings = HibernateSessionFactory.createMappings(this, cfcs);

    DatasourceConnectionPool pool = ((ConfigWebImpl) pc.getConfig()).getDatasourceConnectionPool();
    DatasourceConnection dc =
        pool.getDatasourceConnection(pc, pc.getConfig().getDataSource(dsn), null, null);
    try {
      configuration = HibernateSessionFactory.createConfiguration(this, mappings, dc, ormConf);
    } catch (Exception e) {
      throw Caster.toPageException(e);
    } finally {
      pool.releaseDatasourceConnection(dc);
    }

    addEventListeners(pc, configuration, ormConf, cfcs);

    EntityTuplizerFactory tuplizerFactory = configuration.getEntityTuplizerFactory();
    // tuplizerFactory.registerDefaultTuplizerClass(EntityMode.MAP, CFCEntityTuplizer.class);
    // tuplizerFactory.registerDefaultTuplizerClass(EntityMode.MAP, MapEntityTuplizer.class);
    tuplizerFactory.registerDefaultTuplizerClass(EntityMode.MAP, AbstractEntityTuplizerImpl.class);
    tuplizerFactory.registerDefaultTuplizerClass(EntityMode.POJO, AbstractEntityTuplizerImpl.class);
    // tuplizerFactory.registerDefaultTuplizerClass(EntityMode.POJO,
    // AbstractEntityTuplizerImpl.class);

    // configuration.setEntityResolver(new CFCEntityResolver());
    // configuration.setEntityNotFoundDelegate(new EntityNotFoundDelegate());

    return _factory = configuration.buildSessionFactory();
  }
Ejemplo n.º 6
0
 void checkExistent(PageContext pc, Component cfc) throws ORMException {
   if (!cfcs.containsKey(id(HibernateCaster.getEntityName(cfc))))
     throw new ORMException(
         this, "there is no mapping definition for component [" + cfc.getAbsName() + "]");
 }