Java Code Examples for com.orientechnologies.orient.core.record.impl.ODocument#getSchemaClass()

The following examples show how to use com.orientechnologies.orient.core.record.impl.ODocument#getSchemaClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OLuceneClassIndexManager.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
private void addIndexesEntries(ODocument document) {
  document = checkForLoading(document);

  // STORE THE RECORD IF NEW, OTHERWISE ITS RID
  final OIdentifiable rid = document.getIdentity().isPersistent() ? document.placeholder() : document;

  final OClass cls = document.getSchemaClass();
  if (cls != null) {
    final Collection<OIndex<?>> indexes = cls.getIndexes();
    for (final OIndex<?> index : indexes) {
      final Object key = index.getDefinition().getDocumentValueToIndex(document);
      // SAVE A COPY TO AVOID PROBLEM ON RECYCLING OF THE RECORD
      if (key instanceof Collection) {
        for (final Object keyItem : (Collection<?>) key)
          if (keyItem != null)
            index.put(keyItem, rid);
      } else if (key != null)
        index.put(key, rid);
    }

  }
}
 
Example 2
Source File: OLuceneClassIndexManager.java    From orientdb-lucene with Apache License 2.0 6 votes vote down vote up
private void checkIndexes(ODocument document, TYPE hookType) {
  document = checkForLoading(document);

  final OClass cls = document.getSchemaClass();
  if (cls != null) {
    final Collection<OIndex<?>> indexes = cls.getIndexes();
    switch (hookType) {
    case BEFORE_CREATE:
      checkIndexedPropertiesOnCreation(document, indexes);
      break;
    case BEFORE_UPDATE:
      checkIndexedPropertiesOnUpdate(document, indexes);
      break;
    default:
      throw new IllegalArgumentException("Invalid hook type: " + hookType);
    }
  }
}
 
Example 3
Source File: BpmnHook.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@Override
public RESULT onTrigger(TYPE iType, ORecord iRecord) {
    if (database.getStatus() != STATUS.OPEN)
        return RESULT.RECORD_NOT_CHANGED;

      if (!(iRecord instanceof ODocument))
        return RESULT.RECORD_NOT_CHANGED;

      final ODocument doc = (ODocument) iRecord;
      OClass oClass = doc.getSchemaClass();
      RESULT res = RESULT.RECORD_NOT_CHANGED;
      if(oClass!=null && oClass.isSubClassOf(IEntityHandler.BPM_ENTITY_CLASS)) {
    	  if(iType.equals(TYPE.BEFORE_CREATE)) {
    		  if(doc.field("id")==null) {
    			  doc.field("id", getNextId());
    			  res = RESULT.RECORD_CHANGED;
    		  }
    	  }
    	  RESULT handlerRes = HandlersManager.get().onTrigger(database, doc, iType);
    	  res = (handlerRes == RESULT.RECORD_NOT_CHANGED || handlerRes==null)?res:handlerRes;
      }
      return res;
}
 
Example 4
Source File: ODocumentPivotTableWidget.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@Override
protected String getDefaultSql() {
	ODocument doc = getModelObject();
	OClass oClass = doc.getSchemaClass();
	OProperty multiProperty = null;
	for(OProperty property : oClass.properties()) {
		if(property.getType().isLink() && property.getType().isMultiValue()) {
			multiProperty = property;
			break;
		}
	}
	if(multiProperty!=null) {
		return "select expand("+multiProperty.getName()+") from :doc";
	} else {
		return "select from :doc";
	}
	
}
 
Example 5
Source File: OPropertyValueValidator.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
protected void validateLink(final IValidatable<T> validatable,
		final OProperty p, final Object linkValue) {
	if (linkValue == null)
		validatable.error(newValidationError("nulllink"));
	else {
		ORecord linkedRecord = null;
		if (linkValue instanceof OIdentifiable)
			linkedRecord = ((OIdentifiable) linkValue).getRecord();
		else if (linkValue instanceof String)
			linkedRecord = new ORecordId((String) linkValue).getRecord();
		else
			validatable.error(newValidationError("linkwrong"));

		if (linkedRecord != null && p.getLinkedClass() != null) {
			if (!(linkedRecord instanceof ODocument))
				validatable.error(newValidationError("linktypewrong",
						"linkedClass", p.getLinkedClass(), "identity",
						linkedRecord.getIdentity()));

			final ODocument doc = (ODocument) linkedRecord;

			// AT THIS POINT CHECK THE CLASS ONLY IF != NULL BECAUSE IN CASE
			// OF GRAPHS THE RECORD COULD BE PARTIAL
			if (doc.getSchemaClass() != null
					&& !p.getLinkedClass().isSuperClassOf(
							doc.getSchemaClass()))
				validatable.error(newValidationError("linktypewrong",
						"linkedClass", p.getLinkedClass(), "identity",
						linkedRecord.getIdentity()));

		}
	}
}
 
Example 6
Source File: OPropertyValueValidator.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
protected void validateEmbedded(final IValidatable<T> validatable,
		final OProperty p, final Object fieldValue) {
	if (fieldValue instanceof ORecordId) {
		validatable.error(newValidationError("embeddedRecord"));
		return;
	} else if (fieldValue instanceof OIdentifiable) {
		if (((OIdentifiable) fieldValue).getIdentity().isValid()) {
			validatable.error(newValidationError("embeddedRecord"));
			return;
		}

		final OClass embeddedClass = p.getLinkedClass();
		if (embeddedClass != null) {
			final ORecord rec = ((OIdentifiable) fieldValue).getRecord();
			if (!(rec instanceof ODocument)) {
				validatable.error(newValidationError("embeddedNotDoc"));
				return;
			}

			final ODocument doc = (ODocument) rec;
			if (doc.getSchemaClass() == null
					|| !(doc.getSchemaClass().isSubClassOf(embeddedClass))) {
				validatable.error(newValidationError("embeddedWrongType", "expectedType", embeddedClass.getName()));
				return;
			}
		}

	} else {
		validatable.error(newValidationError("embeddedNotDoc"));
		return;
	}
}
 
Example 7
Source File: ODocumentPropertyLocator.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Override
public void setValue(Object object, Object value, PropertyResolverConverter converter) {
	ODocument doc = toODocument(object);
	if(value!=null && ! OType.isSimpleType(value)) { //Try to convert if type is not simple
		OClass schemaClass = doc.getSchemaClass();
		OProperty property = schemaClass.getProperty(exp);
		if(property!=null) {
			value = converter.convert(value, property.getType().getDefaultJavaType());
		}
	}
	doc.field(exp, value);
}
 
Example 8
Source File: OSchemaUtils.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
/**
 * Returns common {@link OClass} for a set of documents
 * @param it {@link Iterator} over {@link ODocument}s
 * @return common {@link OClass} or null
 */
public static OClass getCommonOClass(Iterator<ODocument> it) {
   	Set<OClass> candidates =  null;
   	OClass ret = null;
   	while(it.hasNext() && (candidates==null || !candidates.isEmpty())) {
   		ODocument doc = it.next();
   		OClass thisOClass = doc.getSchemaClass();
   		if(candidates==null){
   			candidates = new HashSet<OClass>();
   			candidates.add(thisOClass);
   			candidates.addAll(thisOClass.getAllSuperClasses());
   			ret = thisOClass;
   		}
   		else {
   			if(ret!=null && (ret.equals(thisOClass) || ret.isSuperClassOf(thisOClass))) continue;
   			else if(ret!=null && thisOClass.isSuperClassOf(ret)) {
   				ret = thisOClass;
   				candidates.clear();
   				candidates.add(ret);
   				candidates.addAll(ret.getAllSuperClasses());
   			}
   			else {
   				ret = null;
   				candidates.retainAll(thisOClass.getAllSuperClasses());
   			}
   		}
   	}
   	
   	if(ret==null && candidates!=null && !candidates.isEmpty()) {
   		ret = getDeepestOClass(candidates);
   	}
   	
   	return ret;
   }
 
Example 9
Source File: OLuceneClassIndexManager.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
private void updateIndexEntries(ODocument iDocument) {
  iDocument = checkForLoading(iDocument);

  final OClass cls = iDocument.getSchemaClass();
  if (cls == null)
    return;

  final Collection<OIndex<?>> indexes = (Collection<OIndex<?>>) getDatabase().getMetadata().getIndexManager().getIndexes();

  if (!indexes.isEmpty()) {
    final Set<String> dirtyFields = new HashSet<String>(Arrays.asList(iDocument.getDirtyFields()));
    if (!dirtyFields.isEmpty()) {
      for (final OIndex<?> index : indexes) {
        if (index.getInternal() instanceof OLuceneIndex && index.getConfiguration().field("metadata") != null) {
          if (index.getDefinition() instanceof OCompositeIndexDefinition)
            processCompositeIndexUpdate(index, dirtyFields, iDocument);
          else
            processSingleIndexUpdate(index, dirtyFields, iDocument);

          if (iDocument.isTrackingChanges()) {
            iDocument.setTrackingChanges(false);
            iDocument.setTrackingChanges(true);
          }
        }
      }
    }
  }

}
 
Example 10
Source File: OLoggerEventDispatcherModelFactory.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
public OLoggerEventDispatcherModel createEventDispatcherModel(ODocument document) {
    OClass schemaClass = document.getSchemaClass();
    if (schemaClass != null) {
        switch (schemaClass.getName()) {
            case OLoggerEventDispatcherModel.CLASS_NAME:
                return new OLoggerEventDispatcherModel(document);
            case OLoggerEventFilteredDispatcherModel.CLASS_NAME:
                return new OLoggerEventFilteredDispatcherModel(document);
            case OLoggerEventMailDispatcherModel.CLASS_NAME:
                return new OLoggerEventMailDispatcherModel(document);
        }
    }
    return null;
}
 
Example 11
Source File: ReferencesConsistencyHook.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private boolean enter(ODocument doc)
{
	if(doc.getSchemaClass()==null || HOOK_DISABLED.get()) return false;
	List<ODocument> docs = ENTRY_LOCK.get();
	boolean ret = !docs.contains(doc);
	if(ret) docs.add(doc);
	return ret;
}
 
Example 12
Source File: ReferencesConsistencyHook.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
	public void onRecordAfterCreate(ODocument doc) {
		if(enter(doc))
		{
			try
			{
				OClass thisOClass = doc.getSchemaClass();
//				if(thisOClass==null) return;
				Collection<OProperty> refProperties = getCache().get(doc.getSchemaClass());
				for (OProperty oProperty : refProperties)
				{
					OProperty inverseProperty = CustomAttribute.PROP_INVERSE.getValue(oProperty);
					Object value = doc.field(oProperty.getName());
					if(value instanceof OIdentifiable) value = Arrays.asList(value);
					if(inverseProperty!=null && value!=null && value instanceof Collection)
					{
						for(Object otherObj: (Collection<?>)value)
						{
							if(otherObj instanceof OIdentifiable)
							{
								ODocument otherDoc = ((OIdentifiable) otherObj).getRecord();
								addLink(otherDoc, inverseProperty, doc);
							}
						}
					}
				}
			} catch (ExecutionException e)
			{
				LOG.error("Can't update reverse links onCreate", e);
			}
			finally
			{
				exit(doc);
			}
		}
	}
 
Example 13
Source File: ReferencesConsistencyHook.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
	public void onRecordAfterDelete(ODocument doc) {
		if(enter(doc))
		{
			try
			{
				OClass thisOClass = doc.getSchemaClass();
//				if(thisOClass==null) return;
				Collection<OProperty> refProperties = getCache().get(thisOClass);
				for (OProperty oProperty : refProperties)
				{
					OProperty inverseProperty = CustomAttribute.PROP_INVERSE.getValue(oProperty);
					Object value = doc.field(oProperty.getName());
					if(value instanceof OIdentifiable) value = Arrays.asList(value);
					if(inverseProperty!=null && value!=null && value instanceof Collection)
					{
						for(Object otherObj: (Collection<?>)value)
						{
							if(otherObj instanceof OIdentifiable)
							{
								ODocument otherDoc = ((OIdentifiable) otherObj).getRecord();
								removeLink(otherDoc, inverseProperty, doc);
							}
						}
					}
				}
			} catch (ExecutionException e)
			{
				LOG.error("Can't update reverse links onDelete", e);
			}
			finally
			{
				exit(doc);
			}
		}
	}
 
Example 14
Source File: OClassIntrospector.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
@Override
public ODocument getParent(ODocument doc) {
	if(doc==null || doc.getSchemaClass()==null) return null;
	OClass oClass = doc.getSchemaClass();
	OProperty parent = CustomAttribute.PROP_PARENT.getValue(oClass);
	if(parent!=null) {
		OType type = parent.getType();
		Object value = doc.field(parent.getName());
		if(value!=null) {
			switch (type) {
				case LINK:
					return ((OIdentifiable)value).getRecord();
				case LINKLIST:
				case LINKBAG:
				case LINKSET:
					Collection<OIdentifiable> collection =  (Collection<OIdentifiable>)value;
					return !collection.isEmpty()?(ODocument)collection.iterator().next().getRecord():null;
				case LINKMAP:
					Map<?, ?> map = (Map<?, ?>)value;
					value = map.isEmpty()?null:map.values().iterator().next();
					return value instanceof OIdentifiable ? (ODocument)((OIdentifiable)value).getRecord():null;
			default:
				return null;
			}
		}
	}
	return null;
}
 
Example 15
Source File: SaveODocumentCommand.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
public static void realizeMandatory(ODocument doc) {
	OClass oClass = doc.getSchemaClass();
	if(oClass!=null) {
		for(OProperty property : oClass.properties()) {
			if(property.isMandatory() 
					&& Strings.isEmpty(property.getDefaultValue()) 
					&& !doc.containsField(property.getName())) {
				doc.field(property.getName(), (Object) null);
			}
		}
	}
}
 
Example 16
Source File: CalculablePropertiesHook.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
@Override
public void onRecordAfterRead(ODocument iDocument) {
	super.onRecordAfterRead(iDocument);
	OClass oClass = iDocument.getSchemaClass();
	if(oClass!=null)
	{
		List<String> calcProperties = getCalcProperties(iDocument);
		
		if(calcProperties!=null && calcProperties.size()>0)
		{
			for (String calcProperty :calcProperties) {
				//Force calculation. Required for work around issue in OrientDB
				//if(iDocument.field(calcProperty)!=null) continue;
				final OProperty property = oClass.getProperty(calcProperty);
				String script = CustomAttribute.CALC_SCRIPT.getValue(property);
				if(!Strings.isEmpty(script))
				{
					try {
						List<ODocument> calculated;
						if(FULL_QUERY_PATTERN.matcher(script).find())
						{
							calculated = iDocument.getDatabase().query(new OSQLSynchQuery<Object>(script), iDocument);
						}
						else
						{
							script = "select "+script+" as value from "+iDocument.getIdentity();
							calculated = iDocument.getDatabase().query(new OSQLSynchQuery<Object>(script));
						}
						if(calculated!=null && calculated.size()>0)
						{
							OType type = property.getType();
							Object value;
							if(type.isMultiValue())
							{
								final OType linkedType = property.getLinkedType();
								value = linkedType==null
										?calculated
										:Lists.transform(calculated, new Function<ODocument, Object>() {
											
											@Override
											public Object apply(ODocument input) {
												return OType.convert(input.field("value"), linkedType.getDefaultJavaType());
											}
										});
							}
							else
							{
								value = calculated.get(0).field("value");
							}
							value = OType.convert(value, type.getDefaultJavaType());
							Object oldValue = iDocument.field(calcProperty); 
							if (oldValue!=value && (oldValue==null || !oldValue.equals(value))){
								iDocument.field(calcProperty, value);
							}
						}
					} catch (OCommandSQLParsingException e) { //TODO: Refactor because one exception prevent calculation for others
						LOG.warn("Can't parse SQL for calculable property", e);
						iDocument.field(calcProperty, e.getLocalizedMessage());
					}
				}
			}
			
		}
	}
}