Search in sources :

Example 1 with RestContentParameter

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

the class RestContent method insertContent.

private RestContentParameter insertContent(RestContentParameter icp) {
    try {
        String token = icp.getToken();
        if (authtokenlist.checkValidToken(token)) {
            String apikey = icp.getApikey();
            if (apikeyutil.checkApiKey(apikey, "RestService")) {
                CfClass clazz = cfclassService.findByName(icp.getClassname());
                try {
                    CfClasscontent classcontent = cfclasscontentService.findByName(icp.getContentname().trim().replaceAll("\\s+", "_"));
                    LOGGER.warn("Duplicate Classcontent");
                    icp.setReturncode("Duplicate Classcontent");
                } catch (javax.persistence.NoResultException ex) {
                    CfClasscontent newclasscontent = new CfClasscontent();
                    newclasscontent.setName(icp.getContentname().trim().replaceAll("\\s+", "_"));
                    newclasscontent.setClassref(clazz);
                    CfClasscontent newclasscontent2 = cfclasscontentService.create(newclasscontent);
                    hibernateUtil.insertContent(newclasscontent);
                    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));
                            CfAttributcontent newcontent2 = cfattributcontentService.create(newcontent);
                            icp.getAttributmap().put(attribut.getName(), newcontent2.getContentInteger().toString());
                            icp.setReturncode("OK");
                        } else {
                            CfAttributcontent newcontent = new CfAttributcontent();
                            newcontent.setAttributref(attribut);
                            newcontent.setClasscontentref(newclasscontent);
                            newcontent = contentUtil.setAttributValue(newcontent, icp.getAttributmap().get(attribut.getName()));
                            cfattributcontentService.create(newcontent);
                            if (icp.isIndexing()) {
                                contentUtil.indexContent();
                            }
                            icp.setReturncode("OK");
                        }
                    });
                    hibernateUtil.updateContent(newclasscontent);
                }
            } else {
                icp.setReturncode("Wrong API KEY");
            }
        } else {
            icp.setReturncode("Invalid token");
        }
    } catch (javax.persistence.NoResultException ex) {
        LOGGER.error("NoResultException");
        icp.setReturncode("NoResultException");
    }
    return icp;
}
Also used : CfContentversion(io.clownfish.clownfish.dbentities.CfContentversion) CfListService(io.clownfish.clownfish.serviceinterface.CfListService) CfSitecontent(io.clownfish.clownfish.dbentities.CfSitecontent) CfContentversionService(io.clownfish.clownfish.serviceinterface.CfContentversionService) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) AuthTokenList(io.clownfish.clownfish.datamodels.AuthTokenList) ContentUtil(io.clownfish.clownfish.utils.ContentUtil) Autowired(org.springframework.beans.factory.annotation.Autowired) RestContentParameterExt(io.clownfish.clownfish.datamodels.RestContentParameterExt) ClassUtil(io.clownfish.clownfish.utils.ClassUtil) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent) RestContentParameter(io.clownfish.clownfish.datamodels.RestContentParameter) CfClass(io.clownfish.clownfish.dbentities.CfClass) RequestBody(org.springframework.web.bind.annotation.RequestBody) CfClassService(io.clownfish.clownfish.serviceinterface.CfClassService) CfAttributcontentService(io.clownfish.clownfish.serviceinterface.CfAttributcontentService) CfSitecontentService(io.clownfish.clownfish.serviceinterface.CfSitecontentService) CompressionUtils(io.clownfish.clownfish.utils.CompressionUtils) CfClasscontentKeywordService(io.clownfish.clownfish.serviceinterface.CfClasscontentKeywordService) BigInteger(java.math.BigInteger) CfAttributService(io.clownfish.clownfish.serviceinterface.CfAttributService) CfListcontentService(io.clownfish.clownfish.serviceinterface.CfListcontentService) CfClasscontentService(io.clownfish.clownfish.serviceinterface.CfClasscontentService) PostMapping(org.springframework.web.bind.annotation.PostMapping) HibernateUtil(io.clownfish.clownfish.utils.HibernateUtil) Logger(org.slf4j.Logger) CfAttribut(io.clownfish.clownfish.dbentities.CfAttribut) CfAttributetypeService(io.clownfish.clownfish.serviceinterface.CfAttributetypeService) IOException(java.io.IOException) CfListcontent(io.clownfish.clownfish.dbentities.CfListcontent) RestController(org.springframework.web.bind.annotation.RestController) ApiKeyUtil(io.clownfish.clownfish.utils.ApiKeyUtil) List(java.util.List) CfClasscontentkeyword(io.clownfish.clownfish.dbentities.CfClasscontentkeyword) CfAssetService(io.clownfish.clownfish.serviceinterface.CfAssetService) CfContentversionPK(io.clownfish.clownfish.dbentities.CfContentversionPK) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) CfClass(io.clownfish.clownfish.dbentities.CfClass) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) AuthTokenList(io.clownfish.clownfish.datamodels.AuthTokenList) List(java.util.List) CfClasscontent(io.clownfish.clownfish.dbentities.CfClasscontent)

Example 2 with RestContentParameter

use of io.clownfish.clownfish.datamodels.RestContentParameter 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)

Example 3 with RestContentParameter

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

the class ClassUtil method jsonImport.

public List<CfAttributcontent> jsonImport(String jsoncontent) {
    ArrayList<CfAttributcontent> attributcontentlist = new ArrayList<>();
    Gson gson = new Gson();
    RestContentParameter contentparameter = gson.fromJson(jsoncontent, RestContentParameter.class);
    CfClass clazz = cfclassService.findByName(contentparameter.getClassname());
    List<CfAttribut> attributlist = cfattributService.findByClassref(clazz);
    for (String key : contentparameter.getAttributmap().keySet()) {
        CfAttribut attribut = getAttributFromAttributlist(attributlist, key);
        CfAttributcontent attributcontent = new CfAttributcontent();
        attributcontent.setAttributref(attribut);
        switch(attribut.getAttributetype().getName()) {
            case "boolean":
                attributcontent.setContentBoolean(true);
                break;
            case "string":
            case "hashstring":
                attributcontent.setContentString(contentparameter.getAttributmap().get(key));
                break;
            case "integer":
                attributcontent.setContentInteger(BigInteger.valueOf(Long.parseLong(contentparameter.getAttributmap().get(key))));
                break;
            case "real":
                attributcontent.setContentReal(Double.parseDouble(contentparameter.getAttributmap().get(key)));
                break;
            case "htmltext":
            case "text":
            case "markdown":
                attributcontent.setContentText(contentparameter.getAttributmap().get(key));
                break;
            case "datetime":
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.GERMAN);
                Date date;
                try {
                    date = formatter.parse(contentparameter.getAttributmap().get(key));
                    attributcontent.setContentDate(date);
                } catch (ParseException ex) {
                    java.util.logging.Logger.getLogger(ClassUtil.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
            case "media":
                CfAsset asset = cfassetService.findByName(contentparameter.getAttributmap().get(key));
                attributcontent.setContentInteger(BigInteger.valueOf(asset.getId()));
                break;
            case "classref":
                try {
                    CfList list = cflistService.findByName(contentparameter.getAttributmap().get(key));
                    attributcontent.setClasscontentlistref(list);
                } catch (Exception ex) {
                    attributcontent.setClasscontentlistref(null);
                }
                break;
            case "assetref":
                try {
                    CfAssetlist assetlist = cfassetlistService.findByName(contentparameter.getAttributmap().get(key));
                    attributcontent.setAssetcontentlistref(assetlist);
                } catch (Exception ex) {
                    attributcontent.setAssetcontentlistref(null);
                }
                break;
        }
        attributcontentlist.add(attributcontent);
    }
    return attributcontentlist;
}
Also used : CfClass(io.clownfish.clownfish.dbentities.CfClass) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) CfAttribut(io.clownfish.clownfish.dbentities.CfAttribut) CfAssetlist(io.clownfish.clownfish.dbentities.CfAssetlist) Date(java.util.Date) ParseException(java.text.ParseException) IOException(java.io.IOException) CfAsset(io.clownfish.clownfish.dbentities.CfAsset) CfList(io.clownfish.clownfish.dbentities.CfList) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) RestContentParameter(io.clownfish.clownfish.datamodels.RestContentParameter) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with RestContentParameter

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

the class ClassUtil method jsonExport.

public String jsonExport(CfClasscontent classcontent, List<CfAttributcontent> attributcontentlist) {
    RestContentParameter contentparameter = new RestContentParameter();
    contentparameter.setClassname(classcontent.getClassref().getName());
    contentparameter.setContentname(classcontent.getName());
    for (CfAttributcontent attributcontent : attributcontentlist) {
        switch(attributcontent.getAttributref().getAttributetype().getName()) {
            case "boolean":
                if (null != attributcontent.getContentBoolean()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentBoolean().toString());
                }
                break;
            case "string":
                if (null != attributcontent.getContentString()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentString());
                }
                break;
            case "hashstring":
                if (null != attributcontent.getContentString()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentString());
                }
                break;
            case "integer":
                if (null != attributcontent.getContentInteger()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentInteger().toString());
                }
                break;
            case "real":
                if (null != attributcontent.getContentReal()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentReal().toString());
                }
                break;
            case "htmltext":
                if (null != attributcontent.getContentText()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentText());
                }
                break;
            case "text":
                if (null != attributcontent.getContentText()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentText());
                }
                break;
            case "markdown":
                if (null != attributcontent.getContentText()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentText());
                }
                break;
            case "datetime":
                if (null != attributcontent.getContentDate()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getContentDate().toString());
                }
                break;
            case "media":
                if (null != attributcontent.getContentInteger()) {
                    CfAsset asset = cfassetService.findById(attributcontent.getContentInteger().longValue());
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), asset.getName());
                }
                break;
            case "classref":
                if (null != attributcontent.getClasscontentlistref()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getClasscontentlistref().getName());
                }
                break;
            case "assetref":
                if (null != attributcontent.getAssetcontentlistref()) {
                    contentparameter.getAttributmap().put(attributcontent.getAttributref().getName(), attributcontent.getAssetcontentlistref().getName());
                }
                break;
        }
    }
    Gson gson = new Gson();
    return gson.toJson(contentparameter);
}
Also used : CfAsset(io.clownfish.clownfish.dbentities.CfAsset) CfAttributcontent(io.clownfish.clownfish.dbentities.CfAttributcontent) Gson(com.google.gson.Gson) RestContentParameter(io.clownfish.clownfish.datamodels.RestContentParameter)

Example 5 with RestContentParameter

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

the class InsertContent method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    StringBuffer jb = new StringBuffer();
    String line = null;
    try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null) {
            jb.append(line);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage());
    }
    Gson gson = new Gson();
    RestContentParameter icp = gson.fromJson(jb.toString(), RestContentParameter.class);
    insertContent(icp, response);
}
Also used : BufferedReader(java.io.BufferedReader) Gson(com.google.gson.Gson) IOException(java.io.IOException) RestContentParameter(io.clownfish.clownfish.datamodels.RestContentParameter)

Aggregations

RestContentParameter (io.clownfish.clownfish.datamodels.RestContentParameter)5 Gson (com.google.gson.Gson)4 CfAttributcontent (io.clownfish.clownfish.dbentities.CfAttributcontent)4 IOException (java.io.IOException)4 CfAttribut (io.clownfish.clownfish.dbentities.CfAttribut)3 CfClass (io.clownfish.clownfish.dbentities.CfClass)3 CfAsset (io.clownfish.clownfish.dbentities.CfAsset)2 CfClasscontent (io.clownfish.clownfish.dbentities.CfClasscontent)2 CfAssetService (io.clownfish.clownfish.serviceinterface.CfAssetService)2 CfAttributService (io.clownfish.clownfish.serviceinterface.CfAttributService)2 CfAttributcontentService (io.clownfish.clownfish.serviceinterface.CfAttributcontentService)2 CfAttributetypeService (io.clownfish.clownfish.serviceinterface.CfAttributetypeService)2 CfClassService (io.clownfish.clownfish.serviceinterface.CfClassService)2 CfClasscontentService (io.clownfish.clownfish.serviceinterface.CfClasscontentService)2 CfListService (io.clownfish.clownfish.serviceinterface.CfListService)2 ApiKeyUtil (io.clownfish.clownfish.utils.ApiKeyUtil)2 ContentUtil (io.clownfish.clownfish.utils.ContentUtil)2 HibernateUtil (io.clownfish.clownfish.utils.HibernateUtil)2 BufferedReader (java.io.BufferedReader)2 BigInteger (java.math.BigInteger)2