Search in sources :

Example 21 with CfList

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

the class GetContentHibernate 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_klasse.isEmpty()) {
            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] + "_" + counter, 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++;
                }
            });
            Session session_tables = HibernateUtil.getClasssessions().get("tables").getSessionFactory().openSession();
            Query query = getQuery(session_tables, searchmap, inst_klasse);
            try {
                List<Map> contentliste = (List<Map>) query.getResultList();
                session_tables.close();
                int listcounter = 0;
                for (Map content : contentliste) {
                    CfClasscontent cfclasscontent = cfclasscontentService.findById((long) content.get("cf_contentref"));
                    if (null != cfclasscontent) {
                        if (!cfclasscontent.isScrapped()) {
                            listcounter++;
                            if (range_start > 0) {
                                if ((listcounter >= range_start) && (listcounter <= range_end)) {
                                    ContentDataOutput contentdataoutput = new ContentDataOutput();
                                    contentdataoutput.setContent(cfclasscontent);
                                    contentdataoutput.setKeywords(getContentKeywords(cfclasscontent, true));
                                    contentdataoutput.setKeyvals(getContentMap(content));
                                    outputlist.add(contentdataoutput);
                                }
                            } else {
                                ContentDataOutput contentdataoutput = new ContentDataOutput();
                                contentdataoutput.setContent(cfclasscontent);
                                contentdataoutput.setKeywords(getContentKeywords(cfclasscontent, true));
                                contentdataoutput.setKeyvals(getContentMap(content));
                                outputlist.add(contentdataoutput);
                            }
                        }
                    }
                }
                outputmap.put("contentfound", "true");
            } catch (NoResultException ex) {
                session_tables.close();
                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 {
            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) NoResultException(javax.persistence.NoResultException) Session(org.hibernate.Session) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) 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) Query(org.hibernate.query.Query) 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) HibernateUtil(io.clownfish.clownfish.utils.HibernateUtil) 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) BufferedReader(java.io.BufferedReader) CfList(io.clownfish.clownfish.dbentities.CfList) Query(org.hibernate.query.Query) ContentDataOutput(io.clownfish.clownfish.datamodels.ContentDataOutput) Gson(com.google.gson.Gson) NoResultException(javax.persistence.NoResultException) IOException(java.io.IOException) CfListcontent(io.clownfish.clownfish.dbentities.CfListcontent) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) CfList(io.clownfish.clownfish.dbentities.CfList) ArrayList(java.util.ArrayList) List(java.util.List) CfList(io.clownfish.clownfish.dbentities.CfList) HashMap(java.util.HashMap) Map(java.util.Map) Session(org.hibernate.Session) PrintWriter(java.io.PrintWriter)

Example 22 with CfList

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

the class GetDatalist method processRequest.

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param gcp GetContentParameters
 * @param response servlet response
 * @return GetContentParameters
 */
protected GetContentParameter processRequest(GetContentParameter gcp, HttpServletResponse response) {
    DatalistOutput datalistoutput = new DatalistOutput();
    ArrayList<ContentOutput> outputlist = new ArrayList<>();
    outputlist = new ArrayList<>();
    // outputmap = new HashMap<>();
    apikey = gcp.getApikey();
    if (apikeyutil.checkApiKey(apikey, "RestService")) {
        String inst_name = "";
        int range_start = 0;
        int range_end = 0;
        if ((null != gcp.getRange()) && (!gcp.getRange().isEmpty())) {
            if (gcp.getRange().contains("-")) {
                String[] ranges = gcp.getRange().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(gcp.getRange());
                range_end = range_start;
            }
        }
        name = "";
        name = gcp.getListname();
        inst_name = name;
        CfList cflist = cflistService.findByName(inst_name);
        List<CfListcontent> listcontentList = cflistcontentService.findByListref(cflist.getId());
        List<CfClasscontent> classcontentList = new ArrayList<>();
        for (CfListcontent listcontent : listcontentList) {
            CfClasscontent classcontent = cfclasscontentService.findById(listcontent.getCfListcontentPK().getClasscontentref());
            if (null != classcontent) {
                classcontentList.add(classcontent);
            } else {
                LOGGER.warn("Classcontent does not exist: " + inst_name + " - " + listcontent.getCfListcontentPK().getClasscontentref());
            }
        }
        for (CfClasscontent classcontent : classcontentList) {
            List<CfAttributcontent> attributcontentList = cfattributcontentService.findByClasscontentref(classcontent);
            ContentOutput co = new ContentOutput();
            co.setIdentifier(classcontent.getName());
            co.setKeyvals(contentUtil.getContentOutputKeyval(attributcontentList));
            co.setKeywords(contentUtil.getContentOutputKeywords(classcontent, false));
            outputlist.add(co);
        }
        datalistoutput.setCflist(cflist);
        datalistoutput.setOutputlist(outputlist);
        Gson gson = new Gson();
        String json = gson.toJson(datalistoutput);
        gcp.setJson(json);
        gcp.setReturncode("TRUE");
        return gcp;
    } else {
        gcp.setReturncode("FALSE");
        gcp.setJson("[]");
        return gcp;
    }
}
Also used : ArrayList(java.util.ArrayList) 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) DatalistOutput(io.clownfish.clownfish.datamodels.DatalistOutput) ContentOutput(io.clownfish.clownfish.datamodels.ContentOutput)

Example 23 with CfList

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

the class HibernateUtil method fillRelation.

private static void fillRelation(String classname, String attributname, Session session_tables, Session session_relations) {
    CfClass cfclass = cfclassservice.findByName(classname);
    List<CfClasscontent> classcontentlist = cfclasscontentService.findByClassref(cfclass);
    Query q = session_relations.createSQLQuery("TRUNCATE TABLE usr_rel_" + classname + "_" + attributname);
    Transaction txt = session_relations.beginTransaction();
    int count = q.executeUpdate();
    txt.commit();
    for (CfClasscontent classcontent : classcontentlist) {
        LOGGER.info("FILLRELATION:" + classname);
        List<CfAttributcontent> attributcontentlist = cfattributcontentService.findByClasscontentref(classcontent);
        for (CfAttributcontent attributcontent : attributcontentlist) {
            if (0 == attributcontent.getAttributref().getName().compareToIgnoreCase(attributname)) {
                CfList contentclassref = attributcontent.getClasscontentlistref();
                if (null != contentclassref) {
                    // System.out.println(contentclassref.getName());
                    List<CfListcontent> listcontentlist = cflistcontentService.findByListref(contentclassref.getId());
                    for (CfListcontent listcontent : listcontentlist) {
                        Map content = (Map) session_tables.createQuery("FROM " + classname + " c WHERE c.cf_contentref = " + classcontent.getId()).getSingleResult();
                        Map referenz = (Map) session_tables.createQuery("FROM " + attributcontent.getAttributref().getRelationref().getName() + " c WHERE c.cf_contentref = " + listcontent.getCfListcontentPK().getClasscontentref()).getSingleResult();
                        Map entity = new HashMap();
                        Map id = new HashMap();
                        id.put(classname + "_ref", classcontent.getId());
                        id.put(attributname + "_ref", listcontent.getCfListcontentPK().getClasscontentref());
                        entity.put("id_", id);
                        entity.put(classname + "_usr_ref", content.get("cf_id"));
                        entity.put(attributname + "_usr_ref", referenz.get("cf_id"));
                        try {
                            Transaction tx = session_relations.beginTransaction();
                            session_relations.save(classname + "_" + attributname, entity);
                            tx.commit();
                        } catch (Exception ex) {
                            LOGGER.error(ex.getMessage());
                        }
                    }
                }
            }
        }
    }
}
Also used : Query(org.hibernate.query.Query) HashMap(java.util.HashMap) CfClass(io.clownfish.clownfish.dbentities.CfClass) CfListcontent(io.clownfish.clownfish.dbentities.CfListcontent) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) NoResultException(javax.persistence.NoResultException) Transaction(org.hibernate.Transaction) CfList(io.clownfish.clownfish.dbentities.CfList) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with CfList

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

the class SiteUtil method getSitelist_list.

public Map getSitelist_list(List<CfList> sitelist, Map sitecontentmap) {
    if (!sitelist.isEmpty()) {
        for (CfList cflist : sitelist) {
            Map listcontentmap = new LinkedHashMap();
            List<CfListcontent> contentlist = cflistcontentService.findByListref(cflist.getId());
            for (CfListcontent listcontent : contentlist) {
                CfClasscontent classcontent = cfclasscontentService.findById(listcontent.getCfListcontentPK().getClasscontentref());
                cfclassService.findById(classcontent.getClassref().getId());
                List<CfAttributcontent> attributcontentlist = new ArrayList<>();
                attributcontentlist.addAll(cfattributcontentService.findByClasscontentref(classcontent));
                if (0 == useHibernate) {
                    listcontentmap.put(classcontent.getName(), classutil.getattributmap(classcontent));
                } else {
                    listcontentmap.put(classcontent.getName(), hibernateutil.getContent(classcontent.getClassref().getName(), classcontent.getId()));
                }
            }
            sitecontentmap.put(cflist.getName(), listcontentmap);
        }
    }
    return sitecontentmap;
}
Also used : CfList(io.clownfish.clownfish.dbentities.CfList) ArrayList(java.util.ArrayList) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) CfListcontent(io.clownfish.clownfish.dbentities.CfListcontent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) LinkedHashMap(java.util.LinkedHashMap)

Example 25 with CfList

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

the class DataList method onCreateContent.

public void onCreateContent(ActionEvent actionEvent) {
    try {
        contentName = contentName.trim().replaceAll("\\s+", "_");
        cflistService.findByName(contentName);
    } catch (NoResultException ex) {
        CfList newlistcontent = new CfList();
        newlistcontent.setName(contentName);
        newlistcontent.setClassref(selectedClass);
        cflistService.create(newlistcontent);
        onRefreshAll();
    } catch (ConstraintViolationException ex) {
        LOGGER.error(ex.getMessage());
    }
}
Also used : CfList(io.clownfish.clownfish.dbentities.CfList) ConstraintViolationException(jakarta.validation.ConstraintViolationException) NoResultException(javax.persistence.NoResultException)

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