<commit_before>/*
 * Copyright (C) 2013  <NAME>, <NAME>,
 *                     <NAME>, <NAME>, <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package io.car.server.mongo.dao;

import org.bson.types.ObjectId;

import com.github.jmkgreen.morphia.Datastore;
import com.github.jmkgreen.morphia.Key;
import com.github.jmkgreen.morphia.dao.BasicDAO;
import com.github.jmkgreen.morphia.query.Query;
import com.github.jmkgreen.morphia.query.UpdateOperations;
import com.google.inject.Inject;
import com.vividsolutions.jts.geom.Geometry;

import io.car.server.core.dao.TrackDao;
import io.car.server.core.entities.Measurement;
import io.car.server.core.entities.Sensor;
import io.car.server.core.entities.Track;
import io.car.server.core.entities.Tracks;
import io.car.server.core.entities.User;
import io.car.server.mongo.entity.MongoMeasurement;
import io.car.server.mongo.entity.MongoTrack;

/**
 *
 * @author <NAME> <<EMAIL>>
 *
 */
public class MongoTrackDao extends BasicDAO<MongoTrack, ObjectId> implements
        TrackDao {
    @Inject
    public MongoTrackDao(Datastore ds) {
        super(MongoTrack.class, ds);
    }

    @Override
    public Track getById(String id) {
        ObjectId oid;
        try {
            oid = new ObjectId(id);
        } catch (IllegalArgumentException e) {
            return null;
        }
        return get(oid);
    }

    @Override
    public Tracks getByUser(User user) {
        return fetch(createQuery().field(MongoTrack.USER).equal(user));
    }

    @Override
    public Track create(Track track) {
        return save(track);
    }

    @Override
    public void addMeasurement(String track, Measurement m) {
        ObjectId tid = new ObjectId(track);
        Key<MongoTrack> key = new Key<MongoTrack>(MongoTrack.class, tid);
        UpdateOperations<MongoTrack> add = createUpdateOperations()
                .add(MongoTrack.MEASUREMENTS, (MongoMeasurement) m);
        getDatastore().update(key, add);
    }

    @Override
    public MongoTrack save(Track track) {
        MongoTrack mongoTrack = (MongoTrack) track;
        save(mongoTrack);
        return mongoTrack;
    }

    @Override
    public void delete(Track track) {
        delete((MongoTrack) track);
    }

    @Override
    public Tracks getByBbox(double minx, double miny, double maxx, double maxy) {
        Query<MongoTrack> q = createQuery();
        q.field("measurements.location").within(minx, miny, maxx, maxy);
        return fetch(q);
    }

    @Override
    public Tracks getByBbox(Geometry bbox) {
        //FIXME implement
        throw new UnsupportedOperationException("not yet implemented");
    }

    @Override<commit_msg>update modified time while adding measurments to track<commit_after>/*
 * Copyright (C) 2013  <NAME>, <NAME>,
 *                     <NAME>, <NAME>, <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package io.car.server.mongo.dao;

import org.bson.types.ObjectId;
import org.joda.time.DateTime;

import com.github.jmkgreen.morphia.Datastore;
import com.github.jmkgreen.morphia.Key;
import com.github.jmkgreen.morphia.dao.BasicDAO;
import com.github.jmkgreen.morphia.query.Query;
import com.github.jmkgreen.morphia.query.UpdateOperations;
import com.google.inject.Inject;
import com.vividsolutions.jts.geom.Geometry;

import io.car.server.core.dao.TrackDao;
import io.car.server.core.entities.Measurement;
import io.car.server.core.entities.Sensor;
import io.car.server.core.entities.Track;
import io.car.server.core.entities.Tracks;
import io.car.server.core.entities.User;
import io.car.server.mongo.entity.MongoMeasurement;
import io.car.server.mongo.entity.MongoTrack;

/**
 *
 * @author <NAME> <<EMAIL>>
 *
 */
public class MongoTrackDao extends BasicDAO<MongoTrack, ObjectId> implements
        TrackDao {
    @Inject
    public MongoTrackDao(Datastore ds) {
        super(MongoTrack.class, ds);
    }

    @Override
    public Track getById(String id) {
        ObjectId oid;
        try {
            oid = new ObjectId(id);
        } catch (IllegalArgumentException e) {
            return null;
        }
        return get(oid);
    }

    @Override
    public Tracks getByUser(User user) {
        return fetch(createQuery().field(MongoTrack.USER).equal(user));
    }

    @Override
    public Track create(Track track) {
        return save(track);
    }

    @Override
    public void addMeasurement(String track, Measurement m) {
        ObjectId tid = new ObjectId(track);
        Key<MongoTrack> key = new Key<MongoTrack>(MongoTrack.class, tid);
        UpdateOperations<MongoTrack> add = createUpdateOperations()
                .set(MongoTrack.LAST_MODIFIED, new DateTime())
                .add(MongoTrack.MEASUREMENTS, (MongoMeasurement) m);
        getDatastore().update(key, add);
    }

    @Override
    public MongoTrack save(Track track) {
        MongoTrack mongoTrack = (MongoTrack) track;
        save(mongoTrack);
        return mongoTrack;
    }

    @Override
    public void delete(Track track) {
        delete((MongoTrack) track);
    }

    @Override
    public Tracks getByBbox(double minx, double miny, double maxx, double maxy) {
        Query<MongoTrack> q = createQuery();
        q.field("measurements.location").within(minx, miny, maxx, maxy);
        return fetch(q);
    }

    @Override
    public Tracks getByBbox(Geometry bbox) {
        //FIXME implement
        throw new UnsupportedOperationException("not yet implemented");
    }

    @Override
{'fraction_non_alphanumeric': 0.10132420770718643, 'fraction_numerical': 0.00148787382829936, 'mean_word_length': 3.069007263922518, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3226613', 'n_tokens_mistral': 2052, 'n_tokens_neox': 2026, 'n_words': 551}