Search in sources :

Example 1 with Model

use of siena.Model in project siena by mandubian.

the class RemoteStub method process.

@SuppressWarnings("unchecked")
public Document process(Document doc) {
    try {
        Element root = doc.getRootElement();
        if (key != null) {
            // TODO: cutom exception message if time is null
            long time = Long.parseLong(root.attributeValue("time"));
            String hash = root.attributeValue("hash");
            if (!Util.sha1(time + key).equals(hash)) {
                throw new SienaException("Invalid hash");
            }
            long diff = Math.abs(time - System.currentTimeMillis());
            if (diff > 10000) {
                throw new SienaException("Invalid time");
            }
        }
        String action = root.getName();
        if ("insert".equals(action)) {
            Model obj = parseEntity(root, classLoader);
            obj.insert();
            return simpleResponse(obj, true);
        } else if ("update".equals(action)) {
            parseEntity(root, classLoader).update();
        } else if ("delete".equals(action)) {
            parseEntity(root, classLoader).delete();
        } else if ("get".equals(action)) {
            Model obj = parseEntity(root, classLoader);
            obj.get();
            return simpleResponse(obj, false);
        } else if ("query".equals(action)) {
            // TODO: convert document to QueryBase
            String clazzName = root.attributeValue("class");
            Class<? extends Model> clazz = (Class<? extends Model>) Common.classForName(clazzName, classLoader);
            Query<? extends Model> query = Model.all(clazz);
            List<Element> list = root.elements();
            for (Element element : list) {
                String name = element.getName();
                String fieldName = element.attributeValue("field");
                if ("filter".equals(name)) {
                    // TODO: operator attribute
                    Field field = clazz.getField(fieldName);
                    Object value = null;
                    if (element.hasContent()) {
                        if (ClassInfo.isModel(field.getType())) {
                            value = Common.parseEntity(element, classLoader);
                        } else {
                            value = Util.fromString(field.getType(), element.getText());
                        }
                    }
                    query.filter(fieldName, value);
                } else if ("order".equals(name)) {
                    // TODO: ascending attribute
                    query.order(fieldName);
                }
            }
            String limit = root.attributeValue("limit");
            String offset = root.attributeValue("offset");
            List<? extends Model> result = null;
            if (limit != null && offset != null) {
                result = query.fetch(Integer.parseInt(limit), Integer.parseInt(offset));
            } else if (limit != null) {
                result = query.fetch(Integer.parseInt(limit));
            } else {
                result = query.fetch();
            }
            Document response = DocumentHelper.createDocument();
            Element r = response.addElement("result");
            for (Model obj : result) {
                Element object = r.addElement("object");
                fillRequestElement(obj, object, false);
            }
            // TODO add nextOffset to response
            return response;
        }
    } catch (Throwable e) {
        return error(e);
    }
    return newDocument("ok");
}
Also used : Common.fillRequestElement(siena.remote.Common.fillRequestElement) Element(org.dom4j.Element) Document(org.dom4j.Document) Field(java.lang.reflect.Field) Model(siena.Model) SienaException(siena.SienaException)

Example 2 with Model

use of siena.Model in project siena by mandubian.

the class Common method parseEntity.

public static Model parseEntity(Element element, ClassLoader classLoader) {
    String clazzName = element.attributeValue("class");
    Model obj = null;
    try {
        Class<?> clazz = classForName(clazzName, classLoader);
        obj = (Model) clazz.newInstance();
    } catch (Exception e) {
        throw new SienaException("Error while trying to create an instance of " + clazzName + ". " + e.getMessage());
    }
    parseEntity(obj, element, classLoader);
    return obj;
}
Also used : Model(siena.Model) SienaException(siena.SienaException) SienaException(siena.SienaException)

Aggregations

Model (siena.Model)2 SienaException (siena.SienaException)2 Field (java.lang.reflect.Field)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1 Common.fillRequestElement (siena.remote.Common.fillRequestElement)1