Search in sources :

Example 1 with RestKeywordListOutput

use of io.clownfish.clownfish.datamodels.RestKeywordListOutput in project Clownfish by rawdog71.

the class GetKeywordLibraries method doGet.

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
    try {
        String token = request.getParameter("token");
        if (authtokenlist.checkValidToken(token)) {
            String apikey = request.getParameter("apikey");
            if (apikeyutil.checkApiKey(apikey, "RestService")) {
                CfKeywordlist keywordlist = null;
                List<CfKeywordlist> keywordlistList = new ArrayList<>();
                String keywordlistid = request.getParameter("id");
                if (keywordlistid != null) {
                    keywordlist = cfkeywordlistService.findById(Long.parseLong(keywordlistid));
                    keywordlistList.add(keywordlist);
                }
                String keywordlistname = request.getParameter("name");
                if (keywordlistname != null) {
                    keywordlist = cfkeywordlistService.findByName(keywordlistname);
                    keywordlistList.clear();
                    keywordlistList.add(keywordlist);
                }
                if ((null == keywordlistid) && (null == keywordlistname)) {
                    keywordlistList = cfkeywordlistService.findAll();
                }
                ArrayList<RestKeywordListOutput> keywordlistoutputList = new ArrayList<>();
                for (CfKeywordlist keywordlistItem : keywordlistList) {
                    List<CfKeyword> keywordList = new ArrayList<>();
                    List<CfKeywordlistcontent> keywordlistcontentList = cfkeywordlistcontentService.findByKeywordlistref(keywordlistItem.getId());
                    for (CfKeywordlistcontent keywordlistcontent : keywordlistcontentList) {
                        keywordList.add(cfkeywordService.findById(keywordlistcontent.getCfKeywordlistcontentPK().getKeywordref()));
                    }
                    RestKeywordListOutput keywordlistoutput = new RestKeywordListOutput();
                    keywordlistoutput.setKeywordlist(keywordlistItem);
                    keywordlistoutput.setKeywords(keywordList);
                    keywordlistoutputList.add(keywordlistoutput);
                }
                Gson gson = new Gson();
                String json = gson.toJson(keywordlistoutputList);
                response.setContentType("application/json;charset=UTF-8");
                try (PrintWriter out = response.getWriter()) {
                    out.print(json);
                } catch (IOException ex) {
                    LOGGER.error(ex.getMessage());
                }
            } else {
                PrintWriter out = response.getWriter();
                out.print("Wrong API KEY");
            }
        } else {
            PrintWriter out = response.getWriter();
            out.print("Invalid Token");
        }
    } catch (javax.persistence.NoResultException | java.lang.IllegalArgumentException ex) {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.print("No keyword lists");
        } catch (IOException ex1) {
            LOGGER.error(ex1.getMessage());
        }
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage());
    }
}
Also used : CfKeywordlist(io.clownfish.clownfish.dbentities.CfKeywordlist) CfKeywordlistcontent(io.clownfish.clownfish.dbentities.CfKeywordlistcontent) ArrayList(java.util.ArrayList) CfKeyword(io.clownfish.clownfish.dbentities.CfKeyword) Gson(com.google.gson.Gson) IOException(java.io.IOException) RestKeywordListOutput(io.clownfish.clownfish.datamodels.RestKeywordListOutput) PrintWriter(java.io.PrintWriter)

Aggregations

Gson (com.google.gson.Gson)1 RestKeywordListOutput (io.clownfish.clownfish.datamodels.RestKeywordListOutput)1 CfKeyword (io.clownfish.clownfish.dbentities.CfKeyword)1 CfKeywordlist (io.clownfish.clownfish.dbentities.CfKeywordlist)1 CfKeywordlistcontent (io.clownfish.clownfish.dbentities.CfKeywordlistcontent)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1