PageRenderTime 33ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/alaspatial/src/main/java/org/ala/spatial/web/services/JobsController.java

http://alageospatialportal.googlecode.com/
Java | 153 lines | 113 code | 22 blank | 18 comment | 13 complexity | 47fbec115507daf9e059986bc96101b2 MD5 | raw file
  1/**
  2 * ************************************************************************
  3 * Copyright (C) 2010 Atlas of Living Australia All Rights Reserved.
  4 *
  5 * The contents of this file are subject to the Mozilla Public License Version
  6 * 1.1 (the "License"); you may not use this file except in compliance with the
  7 * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
  8 *
  9 * Software distributed under the License is distributed on an "AS IS" basis,
 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 11 * the specific language governing rights and limitations under the License.
 12 * *************************************************************************
 13 */
 14package org.ala.spatial.web.services;
 15
 16import java.util.HashMap;
 17import java.util.Map;
 18import javax.servlet.http.HttpServletRequest;
 19import org.ala.spatial.util.AnalysisQueue;
 20import org.springframework.stereotype.Controller;
 21import org.springframework.web.bind.annotation.*;
 22
 23/**
 24 * Analysis jobs general webservices.
 25 *
 26 * @author Adam
 27 */
 28@Controller
 29public class JobsController {
 30
 31    static HashMap<String, Integer> lastLogPos = new HashMap<String, Integer>();
 32
 33    @RequestMapping(value = "/ws/job", method = RequestMethod.GET)
 34    public @ResponseBody
 35    Map job(@RequestParam(value = "pid", required = true, defaultValue = "") String pid, HttpServletRequest req) {
 36        String fullLog = req.getParameter("log");
 37        Map m = new HashMap<String, String>();
 38        m.put("state", "job does not exist");
 39        try {
 40            String state = AnalysisQueue.getState(pid);
 41            if (state != null) {
 42                m.put("state", state);
 43                m.put("message", AnalysisQueue.getMessage(pid));
 44                m.put("progress", AnalysisQueue.getProgress(pid));
 45                m.put("status", AnalysisQueue.getStatus(pid));
 46
 47                if (fullLog == null) {
 48                    Integer pos = lastLogPos.containsKey(pid) ? lastLogPos.get(pid) : 0;
 49                    String log = AnalysisQueue.getLog(pid);
 50                    if (log == null) {
 51                        log = "";
 52                    }
 53                    m.put("log", log.substring(pos));
 54                    lastLogPos.put(pid, log.length());
 55                } else {
 56                    m.put("log", AnalysisQueue.getLog(pid));
 57                }
 58            }
 59        } catch (Exception e) {
 60            e.printStackTrace();
 61        }
 62
 63        return m;
 64    }
 65
 66    @RequestMapping(value = "/ws/jobs/listwaiting", method = RequestMethod.GET)
 67    public @ResponseBody
 68    String listWaiting() {
 69        try {
 70            String s = AnalysisQueue.listWaiting();
 71            if (s != null) {
 72                return s;
 73            }
 74
 75        } catch (Exception e) {
 76            e.printStackTrace();
 77        }
 78
 79        return "job does not exist";
 80    }
 81
 82    @RequestMapping(value = "/ws/jobs/listrunning", method = RequestMethod.GET)
 83    public @ResponseBody
 84    String listRunning() {
 85        try {
 86            String s = AnalysisQueue.listRunning();
 87            if (s != null) {
 88                return s;
 89            }
 90
 91        } catch (Exception e) {
 92            e.printStackTrace();
 93        }
 94
 95        return "job does not exist";
 96    }
 97
 98    @RequestMapping(value = "/ws/jobs/listfinished", method = RequestMethod.GET)
 99    public @ResponseBody
100    String listFinished() {
101        try {
102            String s = AnalysisQueue.listFinished();
103            if (s != null) {
104                return s;
105            }
106
107        } catch (Exception e) {
108            e.printStackTrace();
109        }
110
111        return "job does not exist";
112    }
113
114    @RequestMapping(value = "/ws/jobs/cancel", method = RequestMethod.GET)
115    public @ResponseBody
116    String cancel(@RequestParam(value = "pid", required = true, defaultValue = "") String pid) {
117        try {
118
119            AnalysisQueue.cancelJob(pid);
120            return "";
121        } catch (Exception e) {
122            e.printStackTrace();
123        }
124
125        return "job does not exist";
126    }
127
128    @RequestMapping(value = "/ws/jobs/inputs", method = RequestMethod.GET)
129    public @ResponseBody
130    String inputs(@RequestParam(value = "pid", required = true, defaultValue = "") String pid) {
131        try {
132            String s = AnalysisQueue.getInputs(pid);
133            return s;
134        } catch (Exception e) {
135            e.printStackTrace();
136        }
137
138        return "job does not exist";
139    }
140
141    @RequestMapping(value = "/ws/jobs/image", method = RequestMethod.GET)
142    public @ResponseBody
143    String image(@RequestParam(value = "pid", required = true, defaultValue = "") String pid) {
144        try {
145            String s = AnalysisQueue.getImage(pid);
146            return s;
147        } catch (Exception e) {
148            e.printStackTrace();
149        }
150
151        return "job does not exist";
152    }
153}