private Document buildDoc(EntityBaseBean bean) throws IcatException {
    Map<Field, Integer> stringFields = ei.getStringFields(bean.getClass());
    StringBuilder sb = new StringBuilder();
    for (Entry<Field, Integer> item : stringFields.entrySet()) {
      Field field = item.getKey();
      Method getter = ei.getGetters(bean.getClass()).get(field);
      try {
        String text = (String) getter.invoke(bean);
        if (text != null) {
          if (sb.length() != 0) {
            sb.append(' ');
          }
          sb.append(text);
        }
      } catch (IllegalArgumentException e) {
        throw new IcatException(IcatExceptionType.INTERNAL, e.getMessage());
      } catch (IllegalAccessException e) {
        throw new IcatException(IcatExceptionType.INTERNAL, e.getMessage());
      } catch (InvocationTargetException e) {
        throw new IcatException(IcatExceptionType.INTERNAL, e.getMessage());
      }
    }

    Document doc = new Document();
    String id = bean.getClass().getSimpleName() + ":" + bean.getId();
    doc.add(new StringField("id", id, Store.YES));
    doc.add(new StringField("entity", bean.getClass().getSimpleName(), Store.NO));
    doc.add(new TextField("all", sb.toString(), Store.NO));
    logger.debug("Created document '" + sb.toString() + "' to index for " + id);
    return doc;
  }
 public void updateDocument(EntityBaseBean bean) throws IcatException {
   Document doc = buildDoc(bean);
   String id = bean.getClass().getSimpleName() + ":" + bean.getId();
   try {
     iwriter.updateDocument(new Term("id", id), doc);
   } catch (IOException e) {
     throw new IcatException(IcatExceptionType.INTERNAL, e.getMessage());
   }
 }
 public void deleteDocument(EntityBaseBean bean) throws IcatException {
   String id = bean.getClass().getSimpleName() + ":" + bean.getId();
   try {
     iwriter.deleteDocuments(new Term("id", id));
     logger.debug("Deleted document: " + id);
   } catch (IOException e) {
     throw new IcatException(IcatExceptionType.INTERNAL, e.getMessage());
   }
 }
示例#4
0
  private void jsonise(EntityBaseBean bean, JsonGenerator gen) throws IcatException {

    gen.write("id", bean.getId())
        .write("createId", bean.getCreateId())
        .write("createTime", bean.getCreateTime().toString())
        .write("modId", bean.getModId())
        .write("modTime", bean.getModTime().toString());

    Class<? extends EntityBaseBean> klass = bean.getClass();
    Map<Field, Method> getters = eiHandler.getGetters(klass);
    Set<Field> atts = eiHandler.getAttributes(klass);
    Set<Field> updaters = eiHandler.getSettersForUpdate(klass).keySet();

    for (Field field : eiHandler.getFields(klass)) {
      Object value = null;
      try {
        value = getters.get(field).invoke(bean);
      } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new IcatException(IcatExceptionType.INTERNAL, e.getClass() + " " + e.getMessage());
      }
      if (value == null) {
        // Ignore null values
      } else if (atts.contains(field)) {
        String type = field.getType().getSimpleName();
        if (type.equals("String")) {
          gen.write(field.getName(), (String) value);
        } else if (type.equals("Integer")) {
          gen.write(field.getName(), value.toString());
        } else if (type.equals("Double")) {
          gen.write(field.getName(), value.toString());
        } else if (type.equals("Long")) {
          gen.write(field.getName(), value.toString());
        } else if (type.equals("boolean")) {
          gen.write(field.getName(), value.toString());
        } else if (field.getType().isEnum()) {
          gen.write(field.getName(), value.toString());
        } else if (type.equals("Date")) {
          synchronized (df8601) {
            gen.write(field.getName(), df8601.format((Date) value));
          }
        } else {
          throw new IcatException(
              IcatExceptionType.INTERNAL, "Don't know how to jsonise field of type " + type);
        }
      } else if (updaters.contains(field)) {
        gen.writeStartObject(field.getName());
        jsonise((EntityBaseBean) value, gen);
        gen.writeEnd();
      } else {
        gen.writeStartArray(field.getName());
        @SuppressWarnings("unchecked")
        List<EntityBaseBean> beans = (List<EntityBaseBean>) value;
        for (EntityBaseBean b : beans) {
          gen.writeStartObject();
          jsonise(b, gen);
          gen.writeEnd();
        }
        gen.writeEnd();
      }
    }
  }