<commit_before>package com.conveyal.datatools.editor.controllers.api;

import com.conveyal.datatools.editor.controllers.Base;
import com.conveyal.datatools.editor.datastore.FeedTx;
import com.conveyal.datatools.editor.datastore.GlobalTx;
import com.conveyal.datatools.editor.datastore.VersionedDataStore;
import com.conveyal.datatools.editor.models.transit.Agency;
import com.conveyal.datatools.manager.models.JsonViews;
import com.conveyal.datatools.manager.utils.json.JsonManager;
import spark.Request;
import spark.Response;

import static spark.Spark.*;

public class AgencyController {
    public static JsonManager<Agency> json =
            new JsonManager<>(Agency.class, JsonViews.UserInterface.class);
    public static Object getAgency(Request req, Response res) {
        String id = req.params("id");
        String feedId = req.queryParams("feedId");
        Object json = null;
        try {
            final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
            if(id != null) {
                if (!tx.agencies.containsKey(id)) {
                    tx.rollback();
                    halt(404);
                }

                json = Base.toJson(tx.agencies.get(id), false);
            }
            else {
                json = Base.toJson(tx.agencies.values(), false);
            }
            
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            halt(400);
        }
        return json;
    }

    public static Object createAgency(Request req, Response res) {
        Agency agency;
        String feedId = req.queryParams("feedId");
        if (feedId == null)
            halt(400, "You must provide a valid feedId");

        try {
            agency = Base.mapper.readValue(req.body(), Agency.class);
            
            final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
            if (tx.agencies.containsKey(agency.id)) {
                tx.rollback();
                halt(400, "Agency " + agency.id + " already exists");
            }
            
            tx.agencies.put(agency.id, agency);
            tx.commit();

            return agency;
        } catch (Exception e) {
            e.printStackTrace();
            halt(400);
        }
        return null;
    }


    public static Object updateAgency(Request req, Response res) {
        Agency agency;
        String id = req.params("id");
        String feedId = req.queryParams("feedId");

        try {
            agency = Base.mapper.readValue(req.body(), Agency.class);
            
            final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
            if(!tx.agencies.containsKey(agency.id)) {
                tx.rollback();
                halt(400);
            }
            
            tx.agencies.put(agency.id, agency);
            tx.commit();

            return Base.toJson(agency, false);
        } catch (Exception e) {
            e.printStackTrace();
            halt(400);
        }
        return null;
    }

    public static Object deleteAgency(Request req, Response res) {
//        GlobalTx tx = VersionedDataStore.getGlobalTx();
        String id = req.params("id");
        String feedId = req.queryParams("feedId");
        if(id == null) {
            halt(400);
        }
        final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
        if(!tx.agencies.containsKey(id)) {
            tx.rollback();
            halt(400);
        }

        tx.agencies.remove(id);
        tx.commit();
        
        return true; // ok();
    }
    
    /** duplicate an agency */
    public static Object duplicateAgency(Request req, Response res) {

        String id = req.params("id");
        String feedId = req.queryParams("feedId");

        // make sure the agency exists
//        GlobalTx gtx = VersionedDataStore.getGlobalTx();
        final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
        if (!tx.agencies.containsKey(id)) {
            tx.rollback();<commit_msg>remove unused globaltx ref<commit_after>package com.conveyal.datatools.editor.controllers.api;

import com.conveyal.datatools.editor.controllers.Base;
import com.conveyal.datatools.editor.datastore.FeedTx;
import com.conveyal.datatools.editor.datastore.GlobalTx;
import com.conveyal.datatools.editor.datastore.VersionedDataStore;
import com.conveyal.datatools.editor.models.transit.Agency;
import com.conveyal.datatools.manager.models.JsonViews;
import com.conveyal.datatools.manager.utils.json.JsonManager;
import spark.Request;
import spark.Response;

import static spark.Spark.*;

public class AgencyController {
    public static JsonManager<Agency> json =
            new JsonManager<>(Agency.class, JsonViews.UserInterface.class);
    public static Object getAgency(Request req, Response res) {
        String id = req.params("id");
        String feedId = req.queryParams("feedId");
        Object json = null;
        try {
            final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
            if(id != null) {
                if (!tx.agencies.containsKey(id)) {
                    tx.rollback();
                    halt(404);
                }

                json = Base.toJson(tx.agencies.get(id), false);
            }
            else {
                json = Base.toJson(tx.agencies.values(), false);
            }
            
            tx.rollback();
        } catch (Exception e) {
            e.printStackTrace();
            halt(400);
        }
        return json;
    }

    public static Object createAgency(Request req, Response res) {
        Agency agency;
        String feedId = req.queryParams("feedId");
        if (feedId == null)
            halt(400, "You must provide a valid feedId");

        try {
            agency = Base.mapper.readValue(req.body(), Agency.class);
            
            final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
            if (tx.agencies.containsKey(agency.id)) {
                tx.rollback();
                halt(400, "Agency " + agency.id + " already exists");
            }
            
            tx.agencies.put(agency.id, agency);
            tx.commit();

            return agency;
        } catch (Exception e) {
            e.printStackTrace();
            halt(400);
        }
        return null;
    }


    public static Object updateAgency(Request req, Response res) {
        Agency agency;
        String id = req.params("id");
        String feedId = req.queryParams("feedId");

        try {
            agency = Base.mapper.readValue(req.body(), Agency.class);
            
            final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
            if(!tx.agencies.containsKey(agency.id)) {
                tx.rollback();
                halt(400);
            }
            
            tx.agencies.put(agency.id, agency);
            tx.commit();

            return Base.toJson(agency, false);
        } catch (Exception e) {
            e.printStackTrace();
            halt(400);
        }
        return null;
    }

    public static Object deleteAgency(Request req, Response res) {
        String id = req.params("id");
        String feedId = req.queryParams("feedId");
        if(id == null) {
            halt(400);
        }
        final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
        if(!tx.agencies.containsKey(id)) {
            tx.rollback();
            halt(400);
        }

        tx.agencies.remove(id);
        tx.commit();
        
        return true; // ok();
    }
    
    /** duplicate an agency */
    public static Object duplicateAgency(Request req, Response res) {

        String id = req.params("id");
        String feedId = req.queryParams("feedId");

        // make sure the agency exists
//        GlobalTx gtx = VersionedDataStore.getGlobalTx();
        final FeedTx tx = VersionedDataStore.getFeedTx(feedId);
        if (!tx.agencies.containsKey(id)) {
            tx.rollback();
{'fraction_non_alphanumeric': 0.11337611677362527, 'fraction_numerical': 0.006795016987542469, 'mean_word_length': 1.8254532527550658, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7253307', 'n_tokens_mistral': 2297, 'n_tokens_neox': 2203, 'n_words': 451}