Search in sources :

Example 16 with CfClasscontent

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

the class GetContentData 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 apikey = request.getParameter("apikey");
        if (apikeyutil.checkApiKey(apikey, "RestService")) {
            CfClasscontent content = null;
            String contentid = request.getParameter("contentid");
            if (contentid != null) {
                content = cfclasscontentService.findById(Long.parseLong(contentid));
            }
            if (null != content) {
                List<CfAttributcontent> attributcontentList = cfattributcontentService.findByClasscontentref(content);
                ArrayList<HashMap> keyvals = contentUtil.getContentOutputKeyval(attributcontentList);
                ArrayList<String> keywords = getAssetKeywords(content, true);
                ContentDataOutput contentdataoutput = new ContentDataOutput();
                contentdataoutput.setContent(content);
                contentdataoutput.setKeywords(keywords);
                contentdataoutput.setKeyvals(keyvals);
                contentdataoutput.setDifference(contentUtil.hasDifference(content));
                Gson gson = new Gson();
                String json = gson.toJson(contentdataoutput);
                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");
        }
    } catch (javax.persistence.NoResultException | java.lang.IllegalArgumentException ex) {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.print("No content");
        } catch (IOException ex1) {
            LOGGER.error(ex1.getMessage());
        }
    } catch (IOException ex) {
        LOGGER.error(ex.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) ContentDataOutput(io.clownfish.clownfish.datamodels.ContentDataOutput) Gson(com.google.gson.Gson) IOException(java.io.IOException) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) PrintWriter(java.io.PrintWriter)

Example 17 with CfClasscontent

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

the class GetContentHibernate 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) {
    String inst_klasse;
    String inst_identifier;
    String inst_datalist;
    String inst_apikey = "";
    HashMap<String, String> searchmap;
    ArrayList<String> searchkeywords;
    HashMap<String, String> outputmap;
    ArrayList<ContentDataOutput> outputlist;
    List<CfListcontent> listcontent = null;
    int range_start = 0;
    int range_end = 0;
    String range;
    range = gcp.getRange() == null ? "" : gcp.getRange();
    if (!range.isEmpty()) {
        if (range.contains("-")) {
            String[] ranges = 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(range);
            range_end = range_start;
        }
    }
    outputlist = new ArrayList<>();
    outputmap = new HashMap<>();
    inst_apikey = gcp.getApikey();
    if (apikeyutil.checkApiKey(inst_apikey, "RestService")) {
        inst_klasse = gcp.getClassname();
        inst_identifier = gcp.getIdentifier();
        if (null == inst_identifier) {
            inst_identifier = "";
        }
        inst_datalist = gcp.getListname();
        listcontent = null;
        if ((null != inst_datalist) && (!inst_datalist.isEmpty())) {
            CfList dataList = cflistService.findByName(inst_datalist);
            listcontent = cflistcontentService.findByListref(dataList.getId());
        }
        searchmap = new HashMap<>();
        String searches = gcp.getSearches();
        if (null != searches) {
            String[] keys = searches.split("\\$");
            int counter = 1;
            for (String key : keys) {
                if ((counter > 0) && ((counter % 2) != 0)) {
                    searchmap.put(keys[counter - 1] + "_" + counter, keys[counter]);
                }
                counter++;
            }
        }
        searchkeywords = new ArrayList<>();
        String keywordlist = gcp.getKeywords();
        if (null != keywordlist) {
            String[] keys = keywordlist.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);
        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);
                    }
                }
            }
        }
        boolean found = true;
        if (!found) {
            outputmap.put("contentfound", "false");
        }
        Gson gson = new Gson();
        String json = gson.toJson(outputlist);
        gcp.setJson(json);
        gcp.setReturncode("TRUE");
        return gcp;
    } else {
        gcp.setReturncode("FALSE");
        gcp.setJson("[]");
        return gcp;
    }
}
Also used : Query(org.hibernate.query.Query) 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) 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)

Example 18 with CfClasscontent

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

the class GetDatalist 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) {
    DatalistOutput datalistoutput = new DatalistOutput();
    ArrayList<ContentOutput> outputlist = new ArrayList<>();
    String inst_apikey = "";
    String inst_name = "";
    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")) {
        name = "";
        parameters.keySet().stream().filter((paramname) -> (paramname.compareToIgnoreCase("name") == 0)).map((paramname) -> parameters.get(paramname)).forEach((values) -> {
            name = values[0];
        });
        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);
        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 : DatalistOutput(io.clownfish.clownfish.datamodels.DatalistOutput) 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) 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) CfClasscontentKeywordService(io.clownfish.clownfish.serviceinterface.CfClasscontentKeywordService) CfAttributService(io.clownfish.clownfish.serviceinterface.CfAttributService) ContentOutput(io.clownfish.clownfish.datamodels.ContentOutput) 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) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) BufferedReader(java.io.BufferedReader) CfList(io.clownfish.clownfish.dbentities.CfList) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) 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) PrintWriter(java.io.PrintWriter)

Example 19 with CfClasscontent

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

the class ContentUtil method setAttributValue.

public CfAttributcontent setAttributValue(CfAttributcontent selectedAttribut, String editContent) {
    if (null == editContent) {
        editContent = "";
    }
    try {
        switch(selectedAttribut.getAttributref().getAttributetype().getName()) {
            case "boolean":
                selectedAttribut.setContentBoolean(Boolean.valueOf(editContent));
                break;
            case "string":
                if (editContent.length() > 255) {
                    editContent = editContent.substring(0, 255);
                }
                if (selectedAttribut.getAttributref().getIdentity() == true) {
                    List<CfClasscontent> classcontentlist2 = cfclasscontentService.findByClassref(selectedAttribut.getClasscontentref().getClassref());
                    boolean found = false;
                    for (CfClasscontent classcontent : classcontentlist2) {
                        try {
                            CfAttributcontent attributcontent = cfattributcontentService.findByAttributrefAndClasscontentref(selectedAttribut.getAttributref(), classcontent);
                            if (attributcontent.getContentString().compareToIgnoreCase(editContent) == 0) {
                                found = true;
                            }
                        } catch (javax.persistence.NoResultException | NullPointerException ex) {
                            LOGGER.error(ex.getMessage());
                        }
                    }
                    if (!found) {
                        selectedAttribut.setContentString(editContent);
                    }
                } else {
                    selectedAttribut.setContentString(editContent);
                }
                break;
            case "hashstring":
                String salt = PasswordUtil.getSalt(30);
                selectedAttribut.setContentString(PasswordUtil.generateSecurePassword(editContent, salt));
                selectedAttribut.setSalt(salt);
                break;
            case "integer":
                selectedAttribut.setContentInteger(BigInteger.valueOf(Long.parseLong(editContent)));
                break;
            case "real":
                selectedAttribut.setContentReal(Double.parseDouble(editContent));
                break;
            case "htmltext":
                selectedAttribut.setContentText(editContent);
                break;
            case "text":
                selectedAttribut.setContentText(editContent);
                break;
            case "markdown":
                selectedAttribut.setContentText(editContent);
                break;
            case "datetime":
                Date datum;
                DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withZone(DateTimeZone.forID("Europe/Berlin"));
                try {
                    datum = DateTime.parse(editContent, fmt).toDate();
                    selectedAttribut.setContentDate(datum);
                } catch (IllegalArgumentException ex) {
                    try {
                        fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(DateTimeZone.forID("Europe/Berlin"));
                        datum = DateTime.parse(editContent, fmt).toDate();
                        selectedAttribut.setContentDate(datum);
                    } catch (IllegalArgumentException ex2) {
                        fmt = DateTimeFormat.forPattern("dd.MM.yyyy HH:mm:ss").withZone(DateTimeZone.forID("Europe/Berlin"));
                        datum = DateTime.parse(editContent, fmt).toDate();
                        selectedAttribut.setContentDate(datum);
                    }
                }
                break;
            case "media":
                if (null != editContent) {
                    try {
                        CfAsset asset = cfassetService.findByName(editContent);
                        selectedAttribut.setContentInteger(BigInteger.valueOf(asset.getId()));
                    } catch (Exception ex) {
                        selectedAttribut.setContentInteger(null);
                        LOGGER.error("INSERTCONTENT: Media " + editContent + " not found!");
                    }
                } else {
                    selectedAttribut.setContentInteger(null);
                }
                break;
            case "classref":
                if (null != editContent) {
                    CfList list_ref = cflistService.findById(Long.parseLong(editContent));
                    selectedAttribut.setClasscontentlistref(list_ref);
                }
                break;
            case "assetref":
                if (null != editContent) {
                    CfAssetlist assetlist_ref = cfassetlistService.findById(Long.parseLong(editContent));
                    selectedAttribut.setAssetcontentlistref(assetlist_ref);
                }
                break;
        }
        selectedAttribut.setIndexed(false);
        return selectedAttribut;
    } catch (NullPointerException ex) {
        LOGGER.warn(ex.getMessage());
        return selectedAttribut;
    }
}
Also used : CfAssetlist(io.clownfish.clownfish.dbentities.CfAssetlist) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) Date(java.util.Date) DataFormatException(java.util.zip.DataFormatException) IOException(java.io.IOException) CfAsset(io.clownfish.clownfish.dbentities.CfAsset) CfList(io.clownfish.clownfish.dbentities.CfList) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 20 with CfClasscontent

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

the class InsertContent method insertContent.

private RestContentParameter insertContent(RestContentParameter icp, HttpServletResponse response) throws IOException {
    try {
        String apikey = icp.getApikey();
        if (apikeyutil.checkApiKey(apikey, "RestService")) {
            CfClass clazz = cfclassService.findByName(icp.getClassname());
            try {
                CfClasscontent classcontent = cfclasscontentService.findByName(icp.getContentname());
                response.getOutputStream().println("Duplicate Classcontent: " + icp.getContentname());
                icp.setReturncode("NOK");
            } catch (javax.persistence.NoResultException ex) {
                try {
                    CfClasscontent newclasscontent = new CfClasscontent();
                    newclasscontent.setName(icp.getContentname());
                    newclasscontent.setClassref(clazz);
                    CfClasscontent newclasscontent2 = cfclasscontentService.create(newclasscontent);
                    hibernateUtil.insertContent(newclasscontent);
                    response.getOutputStream().println(newclasscontent.getName());
                    List<CfAttribut> attributlist = cfattributService.findByClassref(newclasscontent2.getClassref());
                    attributlist.stream().forEach((attribut) -> {
                        if (attribut.getAutoincrementor() == true) {
                            List<CfClasscontent> classcontentlist2 = cfclasscontentService.findByClassref(newclasscontent2.getClassref());
                            long max = 0;
                            int last = classcontentlist2.size();
                            if (1 == last) {
                                max = 0;
                            } else {
                                CfClasscontent classcontent = classcontentlist2.get(last - 2);
                                CfAttributcontent attributcontent = cfattributcontentService.findByAttributrefAndClasscontentref(attribut, classcontent);
                                if (attributcontent.getContentInteger().longValue() > max) {
                                    max = attributcontent.getContentInteger().longValue();
                                }
                            }
                            CfAttributcontent newcontent = new CfAttributcontent();
                            newcontent.setAttributref(attribut);
                            newcontent.setClasscontentref(newclasscontent);
                            newcontent.setContentInteger(BigInteger.valueOf(max + 1));
                            cfattributcontentService.create(newcontent);
                        } else {
                            CfAttributcontent newcontent = new CfAttributcontent();
                            newcontent.setAttributref(attribut);
                            newcontent.setClasscontentref(newclasscontent);
                            newcontent = contentUtil.setAttributValue(newcontent, icp.getAttributmap().get(attribut.getName()));
                            cfattributcontentService.create(newcontent);
                            contentUtil.indexContent();
                        }
                    });
                    hibernateUtil.updateContent(newclasscontent);
                } catch (IOException ex1) {
                    LOGGER.error(ex1.getMessage());
                }
            } catch (IOException ex) {
                LOGGER.error(ex.getMessage());
            }
        } else {
            try {
                response.getOutputStream().println("Wrong API KEY");
            } catch (IOException ex1) {
                LOGGER.error(ex1.getMessage());
            }
        }
    } catch (javax.persistence.NoResultException ex) {
        try {
            response.getOutputStream().println("Class not found: " + icp.getClassname());
        } catch (IOException ex1) {
            LOGGER.error(ex1.getMessage());
        }
    }
    return icp;
}
Also used : CfListService(io.clownfish.clownfish.serviceinterface.CfListService) ServletException(javax.servlet.ServletException) LoggerFactory(org.slf4j.LoggerFactory) ContentUtil(io.clownfish.clownfish.utils.ContentUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) RestContentParameter(io.clownfish.clownfish.datamodels.RestContentParameter) CfClass(io.clownfish.clownfish.dbentities.CfClass) HttpServletRequest(javax.servlet.http.HttpServletRequest) CfClassService(io.clownfish.clownfish.serviceinterface.CfClassService) CfAttributcontentService(io.clownfish.clownfish.serviceinterface.CfAttributcontentService) Gson(com.google.gson.Gson) BigInteger(java.math.BigInteger) CfAttributService(io.clownfish.clownfish.serviceinterface.CfAttributService) CfClasscontentService(io.clownfish.clownfish.serviceinterface.CfClasscontentService) HibernateUtil(io.clownfish.clownfish.utils.HibernateUtil) Logger(org.slf4j.Logger) CfAttribut(io.clownfish.clownfish.dbentities.CfAttribut) HttpServlet(javax.servlet.http.HttpServlet) CfAttributetypeService(io.clownfish.clownfish.serviceinterface.CfAttributetypeService) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) WebServlet(javax.servlet.annotation.WebServlet) ApiKeyUtil(io.clownfish.clownfish.utils.ApiKeyUtil) List(java.util.List) Component(org.springframework.stereotype.Component) CfAssetService(io.clownfish.clownfish.serviceinterface.CfAssetService) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) BufferedReader(java.io.BufferedReader) CfClass(io.clownfish.clownfish.dbentities.CfClass) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) List(java.util.List) IOException(java.io.IOException) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent)

Aggregations

CfClasscontent (io.clownfish.clownfish.dbentities.CfClasscontent)45 CfAttributcontent (io.clownfish.clownfish.dbentities.CfAttributcontent)24 CfListcontent (io.clownfish.clownfish.dbentities.CfListcontent)22 CfList (io.clownfish.clownfish.dbentities.CfList)21 Map (java.util.Map)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)15 Gson (com.google.gson.Gson)13 CfClass (io.clownfish.clownfish.dbentities.CfClass)13 IOException (java.io.IOException)13 List (java.util.List)12 CfAttributService (io.clownfish.clownfish.serviceinterface.CfAttributService)9 CfAttributcontentService (io.clownfish.clownfish.serviceinterface.CfAttributcontentService)9 CfClassService (io.clownfish.clownfish.serviceinterface.CfClassService)9 CfClasscontentService (io.clownfish.clownfish.serviceinterface.CfClasscontentService)9 Autowired (org.springframework.beans.factory.annotation.Autowired)9 CfAttributetypeService (io.clownfish.clownfish.serviceinterface.CfAttributetypeService)8 CfListService (io.clownfish.clownfish.serviceinterface.CfListService)8 Session (org.hibernate.Session)8 Logger (org.slf4j.Logger)8