Package com.erudika.para.core

Examples of com.erudika.para.core.ParaObject


        type = (typeSingular == null) ? type : typeSingular;

        id2 = StringUtils.isBlank(id2) ? params.getFirst(Config._ID) : id2;
        type2 = StringUtils.isBlank(type2) ? params.getFirst(Config._TYPE) : type2;

        ParaObject pobj = Utils.toObject(type);
        pobj.setId(id);
        pobj = dao.read(app.getAppIdentifier(), pobj.getId());

        Pager pager = new Pager();
        pager.setPage(NumberUtils.toLong(params.getFirst("page"), 0));
        pager.setSortby(params.getFirst("sort"));
        pager.setDesc(Boolean.parseBoolean(params.containsKey("desc") ? params.getFirst("desc") : "true"));
        pager.setLimit(NumberUtils.toInt(params.getFirst("limit"), pager.getLimit()));

        String childrenOnly = params.getFirst("childrenonly");

        if (pobj != null) {
          if (POST.equals(ctx.getMethod())) {
            if (id2 != null) {
              String linkid = pobj.link(id2);
              if (linkid == null) {
                return RestUtils.getStatusResponse(Response.Status.BAD_REQUEST,
                    "Failed to create link.");
              } else {
                return Response.ok(linkid).build();
              }
            } else {
              return RestUtils.getStatusResponse(Response.Status.BAD_REQUEST,
                  "Parameters 'type' and 'id' are missing.");
            }
          } else if (GET.equals(ctx.getMethod())) {
            Map<String, Object> result = new HashMap<String, Object>();
            if (type2 != null) {
              if (id2 != null) {
                return Response.ok(pobj.isLinked(type2, id2)).build();
              } else {
                List<ParaObject> items = new ArrayList<ParaObject>();
                if (childrenOnly == null) {
                  if (params.containsKey("count")) {
                    pager.setCount(pobj.countLinks(type2));
                  } else {
                    items = pobj.getLinkedObjects(type2, pager);
                  }
                } else {
                  if (params.containsKey("count")) {
                    pager.setCount(pobj.countChildren(type2));
                  } else {
                    if (params.containsKey("field") && params.containsKey("term")) {
                      items = pobj.getChildren(type2, params.getFirst("field"),
                          params.getFirst("term"), pager);
                    } else {
                      items = pobj.getChildren(type2, pager);
                    }
                  }
                }
                result.put("items", items);
                result.put("totalHits", pager.getCount());
                return Response.ok(result).build();
              }
            } else {
              return RestUtils.getStatusResponse(Response.Status.BAD_REQUEST,
                  "Parameter 'type' is missing.");
            }
          } else if (DELETE.equals(ctx.getMethod())) {
            if (type2 == null && id2 == null) {
              pobj.unlinkAll();
            } else if (type2 != null) {
              if (id2 != null) {
                pobj.unlink(type2, id2);
              } else if (childrenOnly != null) {
                pobj.deleteChildren(type2);
              }
            }
            return Response.ok().build();
          }
        }
View Full Code Here


  }

  private Inflector<ContainerRequestContext, Response> readHandler(final App app, final String type) {
    return new Inflector<ContainerRequestContext, Response>() {
      public Response apply(ContainerRequestContext ctx) {
        ParaObject obj = Utils.toObject(type);
        obj.setId(ctx.getUriInfo().getPathParameters().getFirst(Config._ID));
        return RestUtils.getReadResponse(dao.read(app.getAppIdentifier(), obj.getId()));
      }
    };
  }
View Full Code Here

  }

  private Inflector<ContainerRequestContext, Response> updateHandler(final App app, final String type) {
    return new Inflector<ContainerRequestContext, Response>() {
      public Response apply(ContainerRequestContext ctx) {
        ParaObject obj = Utils.toObject(type);
        obj.setType(type);
        obj.setId(ctx.getUriInfo().getPathParameters().getFirst(Config._ID));
        return RestUtils.getUpdateResponse(app, dao.read(app.getAppIdentifier(), obj.getId()),
            ctx.getEntityStream());
      }
    };
  }
View Full Code Here

  }

  private Inflector<ContainerRequestContext, Response> deleteHandler(final App app, final String type) {
    return new Inflector<ContainerRequestContext, Response>() {
      public Response apply(ContainerRequestContext ctx) {
        ParaObject obj = Utils.toObject(type);
        obj.setType(type);
        obj.setId(ctx.getUriInfo().getPathParameters().getFirst(Config._ID));
        return RestUtils.getDeleteResponse(app, obj);
      }
    };
  }
View Full Code Here

    String appid = AOPUtils.getFirstArgOfString(args);

    if (indexedAnno != null) {
      switch (indexedAnno.action()) {
        case ADD:
          ParaObject addMe = AOPUtils.getArgOfParaObject(args);
          if (Utils.isValidObject(addMe)) {
            result = mi.proceed();
            search.index(appid, addMe);
            logger.debug("{}: Indexed {}->{}", cn, appid, addMe.getId());
          } else {
            logger.debug("{}: Invalid object {}->{}", cn, appid, addMe);
          }
          break;
        case REMOVE:
          result = mi.proceed();
          ParaObject removeMe = AOPUtils.getArgOfParaObject(args);
          search.unindex(appid, removeMe);
          logger.debug("{}: Unindexed {}->{}", cn, appid, (removeMe == null) ? null : removeMe.getId());
          break;
        case ADD_ALL:
          List<ParaObject> addUs = AOPUtils.getArgOfListOfType(args, ParaObject.class);
          removeSpecialClasses(addUs);
          result = mi.proceed();
          search.indexAll(appid, addUs);
          logger.debug("{}: Indexed all {}->#{}", cn, appid, (addUs == null) ? null : addUs.size());
          break;
        case REMOVE_ALL:
          List<ParaObject> removeUs = AOPUtils.getArgOfListOfType(args, ParaObject.class);
          removeSpecialClasses(removeUs);
          result = mi.proceed();
          search.unindexAll(appid, removeUs);
          logger.debug("{}: Unindexed all {}->#{}", cn, appid, (removeUs == null) ? null : removeUs.size());
          break;
        default:
          break;
      }
    }
    if (cachedAnno != null) {
      switch (cachedAnno.action()) {
        case GET:
          String getMeId = (String) args[1];
          if (cache.contains(appid, getMeId)) {
            result = cache.get(appid, getMeId);
            logger.debug("{}: Cache hit: {}->{}", cn, appid, getMeId);
          } else if (getMeId != null) {
            if (result == null) {
              result = mi.proceed();
            }
            if (result != null) {
              cache.put(appid, getMeId, result);
              logger.debug("{}: Cache miss: {}->{}", cn, appid, getMeId);
            }
          }
          break;
        case PUT:
          ParaObject putMe = AOPUtils.getArgOfParaObject(args);
          if (putMe != null) {
            cache.put(appid, putMe.getId(), putMe);
            logger.debug("{}: Cache put: {}->{}", cn, appid, putMe.getId());
          }
          break;
        case DELETE:
          ParaObject deleteMe = AOPUtils.getArgOfParaObject(args);
          if (deleteMe != null) {
            cache.remove(appid, deleteMe.getId());
            logger.debug("{}: Cache delete: {}->{}", cn, appid, deleteMe.getId());
          }
          break;
        case GET_ALL:
          List<String> getUs = AOPUtils.getArgOfListOfType(args, String.class);
          if (getUs != null) {
View Full Code Here

    if (coreTypes.isEmpty()) {
      Set<Class<? extends ParaObject>> coreClasses = new HashSet<Class<? extends ParaObject>>();
      scanForDomainClasses(coreClasses);
      try {
        for (Class<? extends ParaObject> clazz : coreClasses) {
          ParaObject p = clazz.newInstance();
          coreTypes.put(p.getPlural(), p.getType());
        }
      } catch (Exception ex) {
        logger.error(null, ex);
      }
    }
View Full Code Here

   * @param app the app object
   * @return a status code 201 or 400
   */
  @SuppressWarnings("unchecked")
  public static Response getCreateResponse(App app, String type, InputStream is) {
    ParaObject content;
    Response entityRes = getEntity(is, Map.class);
    if (entityRes.getStatusInfo() == Response.Status.OK) {
      Map<String, Object> newContent = (Map<String, Object>) entityRes.getEntity();
      // type is not fount in datatypes (try to get it from req. body)
      if (!StringUtils.isBlank(type)) {
        newContent.put(Config._TYPE, type);
      }
      content = Utils.setAnnotatedFields(newContent);
      content.setAppid(app.getAppIdentifier());
      content.setShardKey(app.isShared() ? app.getAppIdentifier() : null);
      registerNewTypes(app, content);
    } else {
      return entityRes;
    }
    return getCreateResponse(content);
View Full Code Here

    final ArrayList<ParaObject> objects = new ArrayList<ParaObject>();
    Response entityRes = getEntity(is, List.class);
    if (entityRes.getStatusInfo() == Response.Status.OK) {
      List<Map<String, Object>> items = (List<Map<String, Object>>) entityRes.getEntity();
      for (Map<String, Object> object : items) {
        ParaObject pobj = Utils.setAnnotatedFields(object);
        if (pobj != null && Utils.isValidObject(pobj)) {
          pobj.setAppid(app.getAppIdentifier());
          pobj.setShardKey(app.isShared() ? app.getAppIdentifier() : null);
          objects.add(pobj);
        }
      }

      Para.getDAO().createAll(app.getAppIdentifier(), objects);
View Full Code Here

    if (entityRes.getStatusInfo() == Response.Status.OK) {
      List<Map<String, Object>> items = (List<Map<String, Object>>) entityRes.getEntity();
      // WARN: objects will not be validated here as this would require them to be read first
      for (Map<String, Object> item : items) {
        if (item != null && item.containsKey(Config._ID) && item.containsKey(Config._TYPE)) {
          ParaObject pobj = Utils.setAnnotatedFields(null, item, Locked.class);
          if (pobj != null) {
            pobj.setId((String) item.get(Config._ID));
            pobj.setType((String) item.get(Config._TYPE));
            pobj.setShardKey(app.isShared() ? app.getAppIdentifier() : null);
            objects.add(pobj);
          }
        }
      }
      Para.getDAO().updateAll(app.getAppIdentifier(), objects);
View Full Code Here

    if (coreTypes.isEmpty()) {
      Set<Class<? extends ParaObject>> coreClasses = new HashSet<Class<? extends ParaObject>>();
      scanForDomainClasses(coreClasses);
      try {
        for (Class<? extends ParaObject> clazz : coreClasses) {
          ParaObject p = clazz.newInstance();
          coreTypes.put(p.getPlural(), p.getType());
        }
      } catch (Exception ex) {
        logger.error(null, ex);
      }
    }
View Full Code Here

TOP

Related Classes of com.erudika.para.core.ParaObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.