Search in sources :

Example 26 with CfList

use of io.clownfish.clownfish.dbentities.CfList in project Clownfish by rawdog71.

the class GetContent method processRequest.

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
    String inst_klasse;
    String inst_identifier;
    String inst_datalist;
    String inst_range;
    String inst_apikey = "";
    HashMap<String, String> searchmap;
    ArrayList<String> searchkeywords;
    HashMap<String, String> outputmap;
    ArrayList<ContentDataOutput> outputlist;
    List<CfListcontent> listcontent = null;
    int range_start;
    int range_end;
    outputlist = new ArrayList<>();
    outputmap = new HashMap<>();
    Map<String, String[]> parameters = request.getParameterMap();
    parameters.keySet().stream().filter((paramname) -> (paramname.compareToIgnoreCase("apikey") == 0)).map((paramname) -> parameters.get(paramname)).forEach((values) -> {
        apikey = values[0];
    });
    inst_apikey = apikey;
    if (apikeyutil.checkApiKey(inst_apikey, "RestService")) {
        klasse = "";
        parameters.keySet().stream().filter((paramname) -> (paramname.compareToIgnoreCase("class") == 0)).map((paramname) -> parameters.get(paramname)).forEach((values) -> {
            klasse = values[0];
        });
        inst_klasse = klasse;
        identifier = "";
        parameters.keySet().stream().filter((paramname) -> (paramname.compareToIgnoreCase("identifier") == 0)).map((paramname) -> parameters.get(paramname)).forEach((values) -> {
            identifier = values[0];
        });
        inst_identifier = identifier;
        datalist = "";
        parameters.keySet().stream().filter((paramname) -> (paramname.compareToIgnoreCase("datalist") == 0)).map((paramname) -> parameters.get(paramname)).forEach((values) -> {
            datalist = values[0];
        });
        inst_datalist = datalist;
        range = "";
        parameters.keySet().stream().filter((paramname) -> (paramname.compareToIgnoreCase("range") == 0)).map((paramname) -> parameters.get(paramname)).forEach((values) -> {
            range = values[0];
        });
        inst_range = range;
        range_start = 0;
        range_end = 0;
        if (!inst_range.isEmpty()) {
            if (inst_range.contains("-")) {
                String[] ranges = inst_range.split("-");
                range_start = Integer.parseInt(ranges[0]);
                range_end = Integer.parseInt(ranges[1]);
                if (range_start > range_end) {
                    int dummy = range_start;
                    range_start = range_end;
                    range_end = dummy;
                }
            } else {
                range_start = Integer.parseInt(inst_range);
                range_end = range_start;
            }
        }
        listcontent = null;
        if (!inst_datalist.isEmpty()) {
            CfList dataList = cflistService.findByName(inst_datalist);
            listcontent = cflistcontentService.findByListref(dataList.getId());
        }
        searchmap = new HashMap<>();
        parameters.keySet().stream().filter((paramname) -> (paramname.startsWith("search$"))).forEach((paramname) -> {
            String[] keys = paramname.split("\\$");
            int counter = 0;
            for (String key : keys) {
                if ((counter > 0) && ((counter % 2) == 0)) {
                    searchmap.put(keys[counter - 1], keys[counter]);
                }
                counter++;
            }
        });
        searchkeywords = new ArrayList<>();
        parameters.keySet().stream().filter((paramname) -> (paramname.startsWith("keywords"))).forEach((paramname) -> {
            String[] keys = paramname.split("\\$");
            int counter = 0;
            for (String key : keys) {
                if (counter > 0) {
                    searchkeywords.add(key);
                }
                counter++;
            }
        });
        CfClass cfclass = cfclassService.findByName(inst_klasse);
        List<CfClasscontent> classcontentList = cfclasscontentService.findByClassref(cfclass);
        boolean found = true;
        int listcounter = 0;
        for (CfClasscontent classcontent : classcontentList) {
            boolean inList = true;
            // Check if identifier is set and matches classcontent
            if ((!inst_identifier.isEmpty()) && (0 != inst_identifier.compareToIgnoreCase(classcontent.getName()))) {
                inList = false;
            }
            // Check if content is in datalist
            if (null != listcontent) {
                boolean foundinlist = false;
                for (CfListcontent lc : listcontent) {
                    if (lc.getCfListcontentPK().getClasscontentref() == classcontent.getId()) {
                        foundinlist = true;
                        break;
                    }
                }
                inList = foundinlist;
            }
            if (inList) {
                boolean putToList = true;
                List<CfAttributcontent> attributcontentList = cfattributcontentService.findByClasscontentref(classcontent);
                ArrayList<HashMap> keyvals = contentUtil.getContentOutputKeyval(attributcontentList);
                ArrayList<String> keywords = contentUtil.getContentOutputKeywords(classcontent, true);
                if (!searchmap.isEmpty()) {
                    for (String searchcontent : searchmap.keySet()) {
                        String searchvalue = searchmap.get(searchcontent);
                        SearchValues sv = getSearchValues(searchvalue);
                        if (!compareAttribut(keyvals, sv, searchcontent)) {
                            putToList = false;
                            break;
                        }
                    }
                }
                // Check the keyword filter (at least one keyword must be found (OR))
                if (!searchkeywords.isEmpty()) {
                    boolean dummyfound = false;
                    for (String keyword : searchkeywords) {
                        if (keywords.contains(keyword.toLowerCase())) {
                            dummyfound = true;
                        }
                    }
                    putToList = dummyfound;
                }
                if (putToList) {
                    found = true;
                    listcounter++;
                    if (range_start > 0) {
                        if ((listcounter >= range_start) && (listcounter <= range_end)) {
                            ContentDataOutput contentdataoutput = new ContentDataOutput();
                            contentdataoutput.setContent(classcontent);
                            contentdataoutput.setKeywords(keywords);
                            contentdataoutput.setKeyvals(keyvals);
                            outputlist.add(contentdataoutput);
                        // System.out.println(inst_klasse + " - " + listcounter);
                        }
                    } else {
                        ContentDataOutput contentdataoutput = new ContentDataOutput();
                        contentdataoutput.setContent(classcontent);
                        contentdataoutput.setKeywords(keywords);
                        contentdataoutput.setKeyvals(keyvals);
                        outputlist.add(contentdataoutput);
                    // System.out.println(inst_klasse + " - " + listcounter);
                    }
                }
            }
        }
        if (!found) {
            outputmap.put("contentfound", "false");
        }
        Gson gson = new Gson();
        String json = gson.toJson(outputlist);
        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 = null;
        try {
            out = response.getWriter();
            out.print("Wrong API KEY");
        } catch (IOException ex) {
            LOGGER.error(ex.getMessage());
        } finally {
            out.close();
        }
    }
}
Also used : CfKeywordService(io.clownfish.clownfish.serviceinterface.CfKeywordService) CfListService(io.clownfish.clownfish.serviceinterface.CfListService) Setter(lombok.Setter) Getter(lombok.Getter) ServletException(javax.servlet.ServletException) LoggerFactory(org.slf4j.LoggerFactory) ContentUtil(io.clownfish.clownfish.utils.ContentUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) CfClass(io.clownfish.clownfish.dbentities.CfClass) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) CfClassService(io.clownfish.clownfish.serviceinterface.CfClassService) CfAttributcontentService(io.clownfish.clownfish.serviceinterface.CfAttributcontentService) Gson(com.google.gson.Gson) Map(java.util.Map) CfClasscontentKeywordService(io.clownfish.clownfish.serviceinterface.CfClasscontentKeywordService) CfAttributService(io.clownfish.clownfish.serviceinterface.CfAttributService) CfListcontentService(io.clownfish.clownfish.serviceinterface.CfListcontentService) PrintWriter(java.io.PrintWriter) CfClasscontentService(io.clownfish.clownfish.serviceinterface.CfClasscontentService) Logger(org.slf4j.Logger) HttpServlet(javax.servlet.http.HttpServlet) CfAttributetypeService(io.clownfish.clownfish.serviceinterface.CfAttributetypeService) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CfListcontent(io.clownfish.clownfish.dbentities.CfListcontent) GetContentParameter(io.clownfish.clownfish.datamodels.GetContentParameter) WebServlet(javax.servlet.annotation.WebServlet) ApiKeyUtil(io.clownfish.clownfish.utils.ApiKeyUtil) List(java.util.List) Component(org.springframework.stereotype.Component) CfClasscontentkeyword(io.clownfish.clownfish.dbentities.CfClasscontentkeyword) ContentDataOutput(io.clownfish.clownfish.datamodels.ContentDataOutput) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) BufferedReader(java.io.BufferedReader) CfList(io.clownfish.clownfish.dbentities.CfList) HashMap(java.util.HashMap) CfClass(io.clownfish.clownfish.dbentities.CfClass) ContentDataOutput(io.clownfish.clownfish.datamodels.ContentDataOutput) Gson(com.google.gson.Gson) CfListcontent(io.clownfish.clownfish.dbentities.CfListcontent) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) CfList(io.clownfish.clownfish.dbentities.CfList) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) PrintWriter(java.io.PrintWriter) IOException(java.io.IOException)

Example 27 with CfList

use of io.clownfish.clownfish.dbentities.CfList in project Clownfish by rawdog71.

the class RestDatalist method insertDatalist.

private RestDatalistParameter insertDatalist(RestDatalistParameter idp) {
    try {
        String token = idp.getToken();
        if (authtokenlist.checkValidToken(token)) {
            String apikey = idp.getApikey();
            if (apikeyutil.checkApiKey(apikey, "RestService")) {
                try {
                    CfList list = cflistService.findByName(idp.getListname().trim().replaceAll("\\s+", "_"));
                    idp.setReturncode("Duplicate Datalistcontent");
                } catch (javax.persistence.NoResultException ex) {
                    CfClass clazz = cfclassService.findByName(idp.getClassname());
                    CfList newlist = new CfList();
                    newlist.setName(idp.getListname().trim().replaceAll("\\s+", "_"));
                    newlist.setClassref(clazz);
                    CfList newlist2 = cflistService.create(newlist);
                    idp.setListid(newlist2.getId());
                    idp.setReturncode("OK");
                }
            } else {
                idp.setReturncode("Wrong API KEY");
            }
        } else {
            idp.setReturncode("Invalid token");
        }
    } catch (javax.persistence.NoResultException ex) {
        idp.setReturncode("NoResultException");
    }
    return idp;
}
Also used : CfList(io.clownfish.clownfish.dbentities.CfList) CfClass(io.clownfish.clownfish.dbentities.CfClass)

Example 28 with CfList

use of io.clownfish.clownfish.dbentities.CfList in project Clownfish by rawdog71.

the class RestListcontent method insertListcontent.

private RestListcontentParameter insertListcontent(RestListcontentParameter ilcp) {
    try {
        String token = ilcp.getToken();
        if (authtokenlist.checkValidToken(token)) {
            String apikey = ilcp.getApikey();
            if (apikeyutil.checkApiKey(apikey, "RestService")) {
                try {
                    CfList list = cflistService.findByName(ilcp.getListname());
                    CfClasscontent classcontent = cfclasscontentService.findByName(ilcp.getClasscontentname());
                    CfListcontent listcontent = new CfListcontent();
                    CfListcontentPK cfListcontentPK = new CfListcontentPK();
                    cfListcontentPK.setListref(list.getId());
                    cfListcontentPK.setClasscontentref(classcontent.getId());
                    listcontent.setCfListcontentPK(cfListcontentPK);
                    cflistcontentService.create(listcontent);
                    hibernateutil.updateRelation(list);
                    ilcp.setReturncode("OK");
                } catch (Exception ex) {
                    ilcp.setReturncode("Exception");
                }
            } else {
                ilcp.setReturncode("Wrong API KEY");
            }
        } else {
            ilcp.setReturncode("Invalid token");
        }
    } catch (javax.persistence.NoResultException ex) {
        ilcp.setReturncode("NoResultException");
    }
    return ilcp;
}
Also used : CfList(io.clownfish.clownfish.dbentities.CfList) CfListcontent(io.clownfish.clownfish.dbentities.CfListcontent) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) CfListcontentPK(io.clownfish.clownfish.dbentities.CfListcontentPK)

Example 29 with CfList

use of io.clownfish.clownfish.dbentities.CfList in project Clownfish by rawdog71.

the class RestListcontent method deleteListcontent.

private RestListcontentParameter deleteListcontent(RestListcontentParameter ilcp) {
    try {
        String token = ilcp.getToken();
        if (authtokenlist.checkValidToken(token)) {
            String apikey = ilcp.getApikey();
            if (apikeyutil.checkApiKey(apikey, "RestService")) {
                try {
                    ilcp.setReturncode("");
                    CfList list = cflistService.findByName(ilcp.getListname());
                    CfClasscontent classcontent = cfclasscontentService.findByName(ilcp.getClasscontentname());
                    List<CfListcontent> listcontentList = cflistcontentService.findByListref(list.getId());
                    for (CfListcontent listcontent : listcontentList) {
                        if (listcontent.getCfListcontentPK().getClasscontentref() == classcontent.getId()) {
                            cflistcontentService.delete(listcontent);
                            ilcp.setReturncode("OK");
                        }
                    }
                } catch (javax.persistence.EntityExistsException ex) {
                    ilcp.setReturncode("EntityExistsException");
                }
            } else {
                ilcp.setReturncode("Wrong API KEY");
            }
        } else {
            ilcp.setReturncode("Invalid token");
        }
    } catch (javax.persistence.NoResultException ex) {
        ilcp.setReturncode("NoResultException");
    }
    return ilcp;
}
Also used : CfList(io.clownfish.clownfish.dbentities.CfList) CfListcontent(io.clownfish.clownfish.dbentities.CfListcontent) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent)

Aggregations

CfList (io.clownfish.clownfish.dbentities.CfList)29 CfClasscontent (io.clownfish.clownfish.dbentities.CfClasscontent)20 CfListcontent (io.clownfish.clownfish.dbentities.CfListcontent)18 CfAttributcontent (io.clownfish.clownfish.dbentities.CfAttributcontent)12 ArrayList (java.util.ArrayList)12 Gson (com.google.gson.Gson)11 HashMap (java.util.HashMap)10 Map (java.util.Map)10 Session (org.hibernate.Session)10 List (java.util.List)8 IOException (java.io.IOException)7 CfClass (io.clownfish.clownfish.dbentities.CfClass)6 Query (org.hibernate.query.Query)6 GetContentParameter (io.clownfish.clownfish.datamodels.GetContentParameter)5 CfAttributService (io.clownfish.clownfish.serviceinterface.CfAttributService)5 CfAttributcontentService (io.clownfish.clownfish.serviceinterface.CfAttributcontentService)5 CfAttributetypeService (io.clownfish.clownfish.serviceinterface.CfAttributetypeService)5 CfClassService (io.clownfish.clownfish.serviceinterface.CfClassService)5 CfClasscontentKeywordService (io.clownfish.clownfish.serviceinterface.CfClasscontentKeywordService)5 CfClasscontentService (io.clownfish.clownfish.serviceinterface.CfClasscontentService)5