Ejemplo n.º 1
0
 public static String identKey(GroovyObject thisObj, Map<String, Object> cassandraMapping)
     throws IOException, CassandraMappingNullIndexException {
   Object primaryKey = cassandraMapping.get("primaryKey");
   if (primaryKey == null) {
     primaryKey = cassandraMapping.get("unindexedPrimaryKey");
   }
   List<String> names = OrmHelper.stringList(primaryKey);
   List<String> values = new ArrayList<String>(names.size());
   for (String it : names) {
     Object value = thisObj.getProperty(it);
     values.add(KeyHelper.primaryRowKey(value));
   }
   return KeyHelper.makeComposite(values);
 }
Ejemplo n.º 2
0
  public static String primaryRowKey(Object obj)
      throws CassandraMappingNullIndexException, IOException {
    if (obj instanceof String) {
      String str = (String) obj;
      if (str.length() == 0) {
        throw new CassandraMappingNullIndexException(
            "Primary keys and indexed properties cannot have null values");
      } else {
        return str;
      }
    } else if (obj instanceof UUID) {
      UUID id = (UUID) obj;
      if (id.version() == 1) {
        long time = UuidDynamicMethods.time(id);
        return timePrefix(time) + "_" + id.toString();
      } else {
        return id.toString();
      }

    } else if (obj instanceof Date) {
      return timePrefix(((Date) obj).getTime());
    } else if (obj instanceof GroovyObject) {
      GroovyObject g = (GroovyObject) obj;
      if (OrmHelper.isMappedObject(g)) {
        return (String) g.getProperty("id");
      }
    } else if (obj == null) {
      throw new CassandraMappingNullIndexException(
          "Primary keys and indexed properties cannot have null values");
    } else if (obj instanceof Collection) {
      Collection c = (Collection) obj;
      List<String> items = new ArrayList<String>(c.size());
      for (Object it : c) {
        items.add(primaryRowKey(it));
      }
      return makeComposite(items);
    } else if (obj instanceof Number || obj instanceof Boolean) {
      return obj.toString();
    }

    try {
      return String.valueOf(DataMapper.dataProperty(obj));
    } catch (IllegalArgumentException e) {
      // TODO - why do we get this for enums in counters and not in simple properties?
      return obj.toString();
    }
  }
Ejemplo n.º 3
0
 public static List<String> objectIndexRowKeys(List<String> propNames, GroovyObject bean)
     throws IOException {
   try {
     boolean hasNull = false;
     List<Object> valueList = new ArrayList<Object>(propNames.size());
     for (String it : propNames) {
       Object value = bean.getProperty(it);
       if (value != null) {
         valueList.add(value);
       } else {
         hasNull = true;
         break;
       }
     }
     List<String> result;
     if (hasNull) {
       result = new ArrayList<String>();
     } else {
       result = new ArrayList<String>(valueList.size());
       List<List<String>> v2 = OrmHelper.expandNestedArray(valueList);
       for (List values : v2) {
         List<List<Object>> pairs = new ArrayList<List<Object>>();
         int index = 0;
         for (String name : propNames) {
           List<Object> tuple = new ArrayList<Object>(2);
           tuple.add(name);
           tuple.add(values.get(index));
           pairs.add(tuple);
           index++;
         }
         String key = indexRowKey(pairs);
         if (key != null && key.length() > 0) {
           result.add(key);
         }
       }
     }
     return result;
   } catch (CassandraMappingNullIndexException e) {
     return null;
   }
 }