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();
}
}
}
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;
}
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;
}
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;
}
Aggregations