final String entityName = segment;
try {
ModelProvider.getInstance().getEntity(entityName);
} catch (final CheckException ce) {
throw new ResourceNotFoundException("Resource " + entityName + " not found", ce);
}
// now check the second segment and see if an operation is required
String id = null;
boolean countOperation = false;
if (segments.length == 2) {
if (segments[1].equals("count")) {
countOperation = true;
} else {
id = segments[1];
}
}
if (id == null) {
// show all of type entityname
// check if there is a whereClause
final String where = request.getParameter("where");
final String orderBy = request.getParameter("orderBy");
final String firstResult = request.getParameter("firstResult");
final String maxResult = request.getParameter("maxResult");
String whereOrderByClause = "";
if (where != null) {
whereOrderByClause += where;
}
if (orderBy != null && !countOperation) {
whereOrderByClause += " order by " + orderBy;
}
final OBQuery<BaseOBObject> obq = OBDal.getInstance().createQuery(entityName,
whereOrderByClause);
if (firstResult != null) {
try {
obq.setFirstResult(Integer.parseInt(firstResult));
} catch (NumberFormatException e) {
throw new InvalidRequestException("Value of firstResult parameter is not an integer: "
+ firstResult);
}
}
if (maxResult != null) {
try {
obq.setMaxResult(Integer.parseInt(maxResult));
} catch (NumberFormatException e) {
throw new InvalidRequestException("Value of maxResult parameter is not an integer: "
+ firstResult);
}
}
if (countOperation) {
response.setContentType("text/xml;charset=UTF-8");
final String xmlResult = WebServiceUtil.getInstance().createResultXML("" + obq.count());
final Writer w = response.getWriter();
w.write(xmlResult);
w.close();
return;
} else {
final StringWriter sw = new StringWriter();
final EntityXMLConverter exc = EntityXMLConverter.newInstance();
exc.setOptionEmbedChildren(true);
exc.setOptionIncludeChildren(true);
exc.setOptionIncludeReferenced(false);
exc.setOptionExportClientOrganizationReferences(true);
exc.setOutput(sw);
exc.process(obq.list());
xml = sw.toString();
}
} else {
final BaseOBObject result = OBDal.getInstance().get(entityName, id);
if (result == null) {
throw new ResourceNotFoundException("No resource found for entity " + entityName
+ " using id " + id);
}
final StringWriter sw = new StringWriter();
final EntityXMLConverter exc = EntityXMLConverter.newInstance();
exc.setOptionEmbedChildren(true);