Package org.olat.course.statistic.aggregation

Source Code of org.olat.course.statistic.aggregation.SimpleResourceableAggregation

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 1999-2006 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.course.statistic.aggregation;

import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.commons.persistence.DBQuery;
import org.olat.core.logging.activity.LogModule;
import org.olat.course.condition.ExtendedCondition;
import org.olat.course.condition.operators.Operator;

/**
*
* Description:<br>
* The class provides a simple aggregation.
*
* <P>
* Initial Date:  09.12.2009 <br>
* @author bja
*/
public class SimpleResourceableAggregation implements IAggregation {
 
  private Long resId;
  private int aggregation = 0;
  private Date begin;
  private Date end;
  private List<ExtendedCondition> conditions;
  private boolean hasANDConnection;
 
  /**
   * @param resId
   */
  public SimpleResourceableAggregation(Long resId, Date begin, Date end, List<ExtendedCondition> conditions, boolean hasANDConnection) {
    this.resId = resId;
    this.begin = begin;
    this.end = end;
    this.conditions = conditions;
    this.hasANDConnection = hasANDConnection;
   
    executeAggregation();
  }

  private void executeAggregation() {
    int count = 0;

    String query = "select count(v) from org.olat.core.logging.activity.LoggingObject v "
        + "where (v.resourceAdminAction = :resAdminAction) "
        + "AND (v.targetResId = :resId) ";

    // date
    if (begin != null) {
      query = query.concat(" AND (v.creationDate >= :createdAfter)");
    }
    if (end != null) {
      query = query.concat(" AND (v.creationDate < :createdBefore)");
    }
   
    // conditions
    String[] values = new String[conditions.size()];
    if(conditions.size() > 0) {
      query = query.concat(" AND (");
      boolean firstIteration = true;
      int i = 0;
      for(Iterator<ExtendedCondition> iterator = conditions.iterator(); iterator.hasNext();) {
        ExtendedCondition extendedCondition = iterator.next();
        String attribute = extendedCondition.getAttribute();
        int position = LogModule.getUserPropertyPosition(attribute);
        Operator operator = extendedCondition.getOperator();
        String value = extendedCondition.getValue();
        // Is position equal to 0 then the list userProperties doesn't contain the value of attribute.
        if (position > 0 && operator.buildSQLStatement(attribute, value).length() > 0) {
          values[i] = value;
          String connection = "";
          if(!firstIteration) {
            connection = " " + (hasANDConnection?"AND":"OR") + " ";
          }
          query = query.concat(connection + operator.buildSQLStatement("v.userProperty"+position, ":value"+i));
          i++;
        }
        if (firstIteration) firstIteration = false;
      }
      query = query.concat(")");
    }

    DBQuery dbQuery = DBFactory.getInstance().createQuery(query);
    dbQuery.setBoolean("resAdminAction", false);
    dbQuery.setString("resId", Long.toString(resId));
    if (begin != null) {
      dbQuery.setDate("createdAfter", begin);
    }
    if (end != null) {
      Calendar cal = Calendar.getInstance();
      cal.setTime(end);
      cal.add(Calendar.DAY_OF_MONTH, 1);
      end = cal.getTime();
      dbQuery.setDate("createdBefore", end);
    }
   
    if(values.length > 0) {
      int i = 0;
      for (String value : values) {
        dbQuery.setString("value"+i, value);
        i++;
      }
    }

    count = ((Long)dbQuery.list().get(0)).intValue();

    setAggregation(count);
  }

  private void setAggregation(int a) {
    this.aggregation = a;
  }

  @Override
  public int getAggregation() {
    return this.aggregation;
  }

}
TOP

Related Classes of org.olat.course.statistic.aggregation.SimpleResourceableAggregation

TOP
Copyright © 2018 www.massapi.com. 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.