in stack/core/src/main/java/org/apache/usergrid/corepersistence/CpEntityManager.java [2682:2875]
public <A extends Entity> A batchCreate(String entityType, Class<A> entityClass, Map<String, Object> properties,
UUID importId)
throws Exception {
String eType = Schema.normalizeEntityType( entityType );
Schema schema = Schema.getDefaultSchema();
boolean is_application = TYPE_APPLICATION.equals( eType );
if ( ( ( applicationId == null ) || applicationId.equals( UUIDUtils.ZERO_UUID ) ) && !is_application ) {
return null;
}
long timestamp = UUIDUtils.getTimestampInMicros( UUIDUtils.newTimeUUID() );
// if the entity UUID is provided, attempt to get a time from the UUID or from it's created property
if ( importId != null ) {
long timestampFromImport = -1L;
if ( UUIDUtils.isTimeBased( importId ) ) {
timestampFromImport = UUIDUtils.getTimestampInMicros( importId );
}
else if ( properties.get( PROPERTY_CREATED ) != null ) {
// the entity property would be stored as milliseconds
timestampFromImport = getLong( properties.get( PROPERTY_CREATED ) ) * 1000;
}
if (timestampFromImport >= 0){
timestamp = timestampFromImport;
}
}
UUID itemId = UUIDGenerator.newTimeUUID();
if ( is_application ) {
itemId = applicationId;
}
if ( importId != null ) {
itemId = importId;
}
if ( properties == null ) {
properties = new TreeMap<>( CASE_INSENSITIVE_ORDER );
}
if ( entityClass == null ) {
entityClass = ( Class<A> ) Schema.getDefaultSchema().getEntityClass( entityType );
}
Set<String> required = schema.getRequiredProperties( entityType );
if ( required != null ) {
for ( String p : required ) {
if ( !PROPERTY_UUID.equals( p ) && !PROPERTY_TYPE.equals( p ) && !PROPERTY_CREATED.equals( p )
&& !PROPERTY_MODIFIED.equals( p ) ) {
Object v = properties.get( p );
if ( schema.isPropertyTimestamp( entityType, p ) ) {
if ( v == null ) {
properties.put( p, timestamp / 1000 );
}
else {
long ts = getLong( v );
if ( ts <= 0 ) {
properties.put( p, timestamp / 1000 );
}
}
continue;
}
if ( v == null ) {
throw new RequiredPropertyNotFoundException( entityType, p );
}
else if ( ( v instanceof String ) && isBlank( ( String ) v ) ) {
throw new RequiredPropertyNotFoundException( entityType, p );
}
}
}
}
if ( properties.isEmpty() ) {
return null;
}
properties.put( PROPERTY_UUID, itemId );
properties.put( PROPERTY_TYPE, Schema.normalizeEntityType( entityType, false ) );
if ( importId != null ) {
if ( properties.get( PROPERTY_CREATED ) == null ) {
properties.put( PROPERTY_CREATED, ( long ) ( timestamp / 1000 ) );
}
if ( properties.get( PROPERTY_MODIFIED ) == null ) {
properties.put( PROPERTY_MODIFIED, ( long ) ( timestamp / 1000 ) );
}
}
else {
properties.put( PROPERTY_CREATED, ( long ) ( timestamp / 1000 ) );
properties.put( PROPERTY_MODIFIED, ( long ) ( timestamp / 1000 ) );
}
// special case timestamp and published newSettings
// and dictionary their timestamp values if not set
// this is sure to break something for someone someday
if ( properties.containsKey( PROPERTY_TIMESTAMP ) ) {
long ts = getLong( properties.get( PROPERTY_TIMESTAMP ) );
if ( ts <= 0 ) {
properties.put( PROPERTY_TIMESTAMP, ( long ) ( timestamp / 1000 ) );
}
}
A entity = EntityFactory.newEntity( itemId, eType, entityClass );
entity.addProperties( properties );
// logger.info( "Entity created of type {}", entity.getClass().getName() );
if ( Event.ENTITY_TYPE.equals( eType ) ) {
Event event = ( Event ) entity.toTypedEntity();
for ( String prop_name : properties.keySet() ) {
Object propertyValue = properties.get( prop_name );
if ( propertyValue != null ) {
event.setProperty( prop_name, propertyValue );
}
}
Mutator<ByteBuffer> batch = createMutator( cass.getApplicationKeyspace( applicationId ), be );
Message message = storeEventAsMessage( batch, event, timestamp );
incrementEntityCollection( "events", timestamp );
entity.setUuid( message.getUuid() );
batch.execute();
return entity;
}
org.apache.usergrid.persistence.model.entity.Entity cpEntity = entityToCpEntity( entity, importId );
// prepare to write and index Core Persistence Entity into default scope
if ( logger.isTraceEnabled() ) {
logger.trace( "Writing entity {}:{} into app {}\n",
entity.getType(),
entity.getUuid(),
applicationId,
CpEntityMapUtils.toMap( cpEntity ));
}
try {
if ( logger.isTraceEnabled()) {
logger.trace( "About to Write {}:{} version {}",
cpEntity.getId().getType(), cpEntity.getId().getUuid(), cpEntity.getVersion() );
}
String region = lookupAuthoritativeRegionForType( entity.getType() );
//this does the write so before adding to a collection everything already exists already.
cpEntity = ecm.write( cpEntity, region ).toBlocking().last();
entity.setSize(cpEntity.getSize());
if(logger.isTraceEnabled()) {
logger.trace( "Wrote {}:{} version {}",
cpEntity.getId().getType(), cpEntity.getId().getUuid(), cpEntity.getVersion() );
}
}
catch ( WriteUniqueVerifyException wuve ) {
if(logger.isTraceEnabled()){
logger.trace("WriteUniqueVerifyException encountered during batchCreate of entity with id {}",
cpEntity.getId().getUuid());
}
handleWriteUniqueVerifyException( entity, wuve );
}
// reflect changes in the legacy Entity
entity.setUuid( cpEntity.getId().getUuid() );
entity.setProperties( cpEntity );
// add to and index in collection of the application
if ( !is_application) {
updateIndexForEntity(eType, entity, timestamp);
}
//write to our types map
MapManager mm = getMapManagerForTypes();
mm.putString( itemId.toString(), entity.getType() );
return entity;
}