/**
   * See Interface for functional description.
   *
   * @param xmlData aggregationDefinition as xml in aggregationDefinition schema.
   * @return Returns the XML representation of the resource.
   * @throws MissingMethodParameterException ex
   * @throws ScopeNotFoundException ex
   * @throws SystemException ex
   * @see de.escidoc.core.sm.business.interfaces
   *     .AggregationDefinitionHandlerInterface#create(java.lang.String)
   */
  @Override
  @Transactional(rollbackFor = {SystemException.class, RuntimeException.class})
  public String create(final String xmlData)
      throws MissingMethodParameterException, ScopeNotFoundException, SystemException {
    if (xmlData == null || xmlData.length() == 0) {
      throw new MissingMethodParameterException("xml may not be null");
    }

    // parse
    final StaxParser sp = new StaxParser();
    final AggregationDefinitionStaxHandler handler = new AggregationDefinitionStaxHandler(sp);
    sp.addHandler(handler);
    try {
      sp.parse(xmlData);
    } catch (final Exception e) {
      throw new SystemException(e);
    }

    final String scopeId = handler.getAggregationDefinition().getScope().getId();
    final Scope scope = scopesDao.retrieve(scopeId);

    // get AggregationDefinitionObject to insert aggregation-definition
    // into database
    final AggregationDefinition aggregationDefinition = handler.getAggregationDefinition();
    aggregationDefinition.setCreatorId(utility.getCurrentUserId());
    aggregationDefinition.setCreationDate(new Timestamp(System.currentTimeMillis()));
    aggregationDefinition.setScope(scope);

    dao.save(aggregationDefinition);
    handler.setAggregationDefinition(aggregationDefinition);

    // AggregationStatisticDataSelectors
    for (final AggregationStatisticDataSelector selector :
        handler.getAggregationStatisticDataSelectors()) {
      dao.save(selector);
    }
    aggregationDefinition.setAggregationStatisticDataSelectors(
        handler.getAggregationStatisticDataSelectors());

    // AggregationTables
    for (final AggregationTable aggregationTable : handler.getAggregationTables()) {
      dao.save(aggregationTable);
    }
    aggregationDefinition.setAggregationTables(handler.getAggregationTables());

    // Get databaseTableVos for all Aggregation-Tables
    // defined in Aggregation Definition
    final Collection<DatabaseTableVo> databaseTableVos =
        generateAggregationDatabaseTableVos(aggregationDefinition);
    if (databaseTableVos != null) {
      for (final DatabaseTableVo databaseTableVo : databaseTableVos) {
        // create aggregation table in Database
        dbAccessor.createTable(databaseTableVo);
      }
    }

    return renderer.render(aggregationDefinition);
  }
  /**
   * See Interface for functional description.
   *
   * @param parameters filter as CQL query
   * @return Returns the XML representation of the resource-list.
   * @throws InvalidSearchQueryException thrown if the given search query could not be translated
   *     into a SQL query
   * @throws SystemException e.
   * @see de.escidoc.core.sm.business.interfaces .AggregationDefinitionHandlerInterface
   *     #retrieveAggregationDefinitions(java.util.Map)
   */
  @Override
  public String retrieveAggregationDefinitions(final Map<String, String[]> parameters)
      throws InvalidSearchQueryException, SystemException {
    final String result;
    final SRURequestParameters params = new DbRequestParameters(parameters);
    final String query = params.getQuery();
    final int limit = params.getMaximumRecords();
    final int offset = params.getStartRecord();

    if (params.isExplain()) {
      final Map<String, Object> values = new HashMap<String, Object>();

      values.put("PROPERTY_NAMES", new AggregationDefinitionFilter(null).getPropertyNames());
      result = ExplainXmlProvider.getInstance().getExplainAggregationDefinitionXml(values);
    } else if (limit == 0) {
      result =
          renderer.renderAggregationDefinitions(
              new ArrayList<AggregationDefinition>(0), params.getRecordPacking());
    } else {
      // get all scope ids from database
      final List<String> scopeIds = scopesDao.retrieveScopeIds();

      Collection<String> filteredScopeIds = null;

      if (scopeIds != null && !scopeIds.isEmpty()) {
        // get scope-ids filtered by user-privileges
        filteredScopeIds =
            filterUtility.filterRetrievePrivilege(Constants.SCOPE_OBJECT_TYPE, scopeIds);
      }
      Collection<AggregationDefinition> aggregationDefinitions = null;
      if (filteredScopeIds != null && !filteredScopeIds.isEmpty()) {
        // get aggregation-definitions as XML
        aggregationDefinitions =
            dao.retrieveAggregationDefinitions(filteredScopeIds, query, offset, limit);
      }

      result =
          renderer.renderAggregationDefinitions(aggregationDefinitions, params.getRecordPacking());
    }
    return result;
  }