package org.orienteer.bpm.camunda.handler;

import com.orientechnologies.orient.core.metadata.schema.OType;
import org.camunda.bpm.engine.impl.db.ListQueryParameterObject;
import org.camunda.bpm.engine.impl.persistence.entity.MeterLogEntity;
import org.orienteer.bpm.camunda.OPersistenceSession;
import org.orienteer.core.util.OSchemaHelper;

import java.util.Date;
import java.util.Map;

/**
 * Created by kir on 11.07.16.
 */
public class MeterLogEntityHandler extends AbstractEntityHandler<MeterLogEntity> {

    public MeterLogEntityHandler() {
        super("BPMMeterLog");
    }

    @Override
    public void applySchema(OSchemaHelper helper) {
        super.applySchema(helper);

        helper.oProperty("name", OType.STRING, 10)
                .oProperty("reporter", OType.STRING, 20)
                .oProperty("value", OType.INTEGER, 30)
                .oProperty("timestamp", OType.DATETIME, 40);
    }

    @Statement
    public Long selectMeterLogSum(OPersistenceSession session, final ListQueryParameterObject params) {
        Map<String, Object> map = (Map<String, Object>) params.getParameter();
        String query = "select SUM(value) from " + getSchemaClass() + " where";
        if (map.containsKey("name") && map.get("name") != null)
            query += " and name = " + map.get("name").toString();
        if (map.containsKey("reporter") && map.get("reporter") != null)
            query += " and reporter = " + map.get("reporter").toString();
        if (map.containsKey("startDate") && map.get("startDate") != null)
            query += " and timestamp > " + new Date(map.get("startDate").toString());
        if (map.containsKey("endDate") && map.get("endDate") != null)
            query += " and timestamp < " + new Date(map.get("endDate").toString());

        return Long.valueOf(queryList(session, query).get(0).toString());
    }
}

@@ -1584,32 +1584,45 @@
  %3E %22 + new Date(
+Long.valueOf(
 map.get(%22startDa
@@ -1633,24 +1633,25 @@
 .toString())
+)
 ;%0A        if
@@ -1762,16 +1762,29 @@
 ew Date(
+Long.valueOf(
 map.get(
@@ -1805,16 +1805,17 @@
 tring())
+)
 ;%0A%0A     

fix date defect