Exemplo n.º 1
0
  public void deleteEvent(Event event) {
    List<User> list = getUsers();
    Collection<Activity> activities = event.getActivities();
    Set<Activity> tmpActivities = new HashSet<Activity>();
    for (Activity activityEntity : activities) {
      tmpActivities.add(activityEntity);
    }
    for (User user : list) {
      boolean userUpdated = false;
      if (user.getEvents().contains(event)) {
        for (Activity activity : tmpActivities) {
          user.getActivities().remove(activity);
          event.getActivities().remove(activity);
        }
        user.getEvents().remove(event);
        userUpdated = true;
      }
      if (userUpdated) {
        updateObject(user);
      }
    }

    deleteObject(event);
    //        for (Activity activity : tmpActivities) {
    //            deleteObject(activity);
    //        }
  }
  /* (non-Javadoc)
   * @see org.grails.orm.hibernate.cfg.GrailsDomainConfiguration#addDomainClass(org.codehaus.groovy.grails.commons.GrailsDomainClass)
   */
  public GrailsDomainConfiguration addDomainClass(GrailsDomainClass domainClass) {
    if (shouldMapWithGorm(domainClass)) {
      domainClasses.add(domainClass);
    } else {
      addAnnotatedClass(domainClass.getClazz());
    }

    return this;
  }
  /**
   * internal work for responding to a save or update request. This method will add new bundles data
   * if it doesn't exist, otherwise updates the data preserving any current value if its been
   * modified. This approach allows for upgrades to automatically detect and persist new keys, as
   * well as updating any default values that flow in from an upgrade.
   *
   * @param baseName
   * @param moduleName
   * @param newBundle
   * @param loc
   */
  protected void saveOrUpdateInternal(
      String baseName, String moduleName, Map<String, String> newBundle, String loc) {
    String keyName = getIndexKeyName(baseName, moduleName, loc);
    if (indexedList.contains(keyName)) {
      logger.debug("skip saveOrUpdate() as its already happened once for :" + keyName);
      return;
    }

    Set<Entry<String, String>> entrySet = newBundle.entrySet();
    Iterator<Entry<String, String>> entries = entrySet.iterator();

    while (entries.hasNext()) {
      Entry<String, String> entry = entries.next();
      String key = entry.getKey();
      MessageBundleProperty mbp = new MessageBundleProperty();
      mbp.setBaseName(baseName);
      mbp.setModuleName(moduleName);
      mbp.setLocale(loc.toString());
      mbp.setPropertyName(key);
      mbp.setDefaultValue(entry.getValue());
      MessageBundleProperty existingMbp = getProperty(mbp);
      if (existingMbp != null) {
        // don"t update id or value, we don't want to loose that data
        BeanUtils.copyProperties(mbp, existingMbp, new String[] {"id", "value"});
        logger.debug(
            "updating message bundle data for : "
                + getIndexKeyName(mbp.getBaseName(), mbp.getModuleName(), mbp.getLocale()));
        updateMessageBundleProperty(existingMbp);
      } else {
        logger.debug(
            "adding message bundle data for : "
                + getIndexKeyName(mbp.getBaseName(), mbp.getModuleName(), mbp.getLocale()));
        updateMessageBundleProperty(mbp);
      }
    }

    indexedList.add(getIndexKeyName(baseName, moduleName, loc));
  }
  /**
   * schedule timer task to save/update the bundle data. We are using Timer to offload the work,
   * otherwise intial loads of tools will appear very slow, this way it happens in the background.
   * In the original rSmart impl JMS was used, but since the MessageService is in contrib not core
   * we need another solution to avoid that dependency. Currently we are using a java.util.Timer and
   * scheduled Timer task to queue up and process the calls to this method. This is a similar
   * strategy to that used in BaseDigestService.
   *
   * @param baseName
   * @param moduleName
   * @param newBundle
   * @param loc
   */
  public void saveOrUpdate(
      String baseName, String moduleName, ResourceBundle newBundle, Locale loc) {
    String keyName = getIndexKeyName(baseName, moduleName, loc.toString());
    if (indexedList.contains(keyName)) {
      logger.debug("skip saveOrUpdate() as its already happened once for :" + keyName);
      return;
    }

    if (scheduleSaves) {
      queueBundle(baseName, moduleName, convertResourceBundleToMap(newBundle), loc);
    } else {
      saveOrUpdateInternal(
          baseName, moduleName, convertResourceBundleToMap(newBundle), loc.toString());
    }
  }
  /** Overrides the default behaviour to including binding of Grails domain classes. */
  @Override
  protected void secondPassCompile() throws MappingException {
    final Thread currentThread = Thread.currentThread();
    final ClassLoader originalContextLoader = currentThread.getContextClassLoader();
    if (!configLocked) {
      if (LOG.isDebugEnabled())
        LOG.debug(
            "[GrailsAnnotationConfiguration] ["
                + domainClasses.size()
                + "] Grails domain classes to bind to persistence runtime");

      // do Grails class configuration
      configureDomainBinder(binder, grailsApplication, domainClasses);

      for (GrailsDomainClass domainClass : domainClasses) {

        final String fullClassName = domainClass.getFullName();

        String hibernateConfig = fullClassName.replace('.', '/') + ".hbm.xml";
        final ClassLoader loader = originalContextLoader;
        // don't configure Hibernate mapped classes
        if (loader.getResource(hibernateConfig) != null) continue;

        final Mappings mappings = super.createMappings();
        if (!GrailsHibernateUtil.usesDatasource(domainClass, dataSourceName)) {
          continue;
        }

        LOG.debug(
            "[GrailsAnnotationConfiguration] Binding persistent class [" + fullClassName + "]");

        Mapping m = binder.getMapping(domainClass);
        mappings.setAutoImport(m == null || m.getAutoImport());
        binder.bindClass(domainClass, mappings, sessionFactoryBeanName);
      }
    }

    try {
      currentThread.setContextClassLoader(grailsApplication.getClassLoader());
      super.secondPassCompile();
      createSubclassForeignKeys();
    } finally {
      currentThread.setContextClassLoader(originalContextLoader);
    }

    configLocked = true;
  }