Esempio n. 1
0
  public Component getEntityByCFCName(String cfcName, boolean unique) throws PageException {
    String name = cfcName;
    int pointIndex = cfcName.lastIndexOf('.');
    if (pointIndex != -1) {
      name = cfcName.substring(pointIndex + 1);
    } else cfcName = null;

    ComponentPro cfc;
    String[] names = null;
    // search array (array exist when cfcs is in generation)

    if (arr != null) {
      names = new String[arr.size()];
      int index = 0;
      Iterator<ComponentPro> it2 = arr.iterator();
      while (it2.hasNext()) {
        cfc = it2.next();
        names[index++] = cfc.getName();
        if (isEntity(cfc, cfcName, name)) // if(cfc.equalTo(name))
        return unique ? (Component) cfc.duplicate(false) : cfc;
      }
    } else {
      // search cfcs
      Iterator<Entry<String, CFCInfo>> it = cfcs.entrySet().iterator();
      Entry<String, CFCInfo> entry;
      while (it.hasNext()) {
        entry = it.next();
        cfc = entry.getValue().getCFC();
        if (isEntity(cfc, cfcName, name)) // if(cfc.instanceOf(name))
        return unique ? (Component) cfc.duplicate(false) : cfc;

        // if(name.equalsIgnoreCase(HibernateCaster.getEntityName(cfc)))
        //	return cfc;
      }
      names = cfcs.keySet().toArray(new String[cfcs.size()]);
    }

    // search by entityname //TODO is this ok?
    CFCInfo info = cfcs.get(name.toLowerCase());
    if (info != null) {
      cfc = info.getCFC();
      return unique ? (Component) cfc.duplicate(false) : cfc;
    }

    throw new ORMException(
        this,
        "entity ["
            + name
            + "] "
            + (StringUtil.isEmpty(cfcName) ? "" : "with cfc name [" + cfcName + "] ")
            + "does not exist, existing  entities are ["
            + railo.runtime.type.List.arrayToList(names, ", ")
            + "]");
  }
Esempio n. 2
0
 private ComponentPro _create(PageContext pc, String entityName, boolean unique)
     throws PageException {
   CFCInfo info = cfcs.get(id(entityName));
   if (info != null) {
     ComponentPro cfc = info.getCFC();
     if (unique) {
       cfc = (ComponentPro) cfc.duplicate(false);
       if (cfc.contains(pc, INIT)) cfc.call(pc, "init", new Object[] {});
     }
     return cfc;
   }
   return null;
 }
Esempio n. 3
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));
    }
  }
Esempio n. 4
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));
  }
Esempio n. 5
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");
  }
Esempio n. 6
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();
  }