Package org.olat.course.statistic.aggregation

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

/**
* 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.core.logging.activity.LoggingObject;
import org.olat.course.condition.ExtendedCondition;
import org.olat.course.condition.operators.Operator;
import org.olat.course.nodes.CourseNodeFactory;

/**
*
* Description:<br>
* The class provides a simple node aggregation.
*
* <P>
* Initial Date:  10.12.2009 <br>
* @author bja
*/
public class SimpleNodeAggregation implements IAggregation {
 
  private Long resId;
  private int aggregation = 0;
  private Date begin;
  private Date end;
  private List<ExtendedCondition> conditions;
  private boolean hasANDConnection;
 
  /**
   * @param resId
   * @param begin
   * @param end
   * @param conditions
   * @param hasANDConnection
   */
  public SimpleNodeAggregation(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;
   
    calculate();
  }

  private void calculate() {
    // get all LoggingObjects which have a target or parent resource from type CourseNode
    List loggingObjects = executeAggregation();
    // get registered node aliases like fo, den, st, and so on
    List<String> courseNodeAliases = CourseNodeFactory.getInstance().getRegisteredCourseNodeAliases();
    // count all LoggingObjects which have not a course node resourceable type two times 
    int count = 0;
    for (Object o : loggingObjects) {
      LoggingObject loggingObject = (LoggingObject) o;
      if(!(courseNodeAliases.contains(loggingObject.getTargetResType()) && courseNodeAliases.contains(loggingObject.getParentResType()))) {
        count++;
      }
    }
    setAggregation(count);
  }

  private List executeAggregation() {

    String query = "select v from org.olat.core.logging.activity.LoggingObject v "
        + "where (v.resourceAdminAction = :resAdminAction) "
        + "AND ((v.targetResId = :resId) OR (v.parentResId = :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++;
      }
    }

    return dbQuery.list();
  }

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

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

}
TOP

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

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.