Java Code Examples for com.orientechnologies.orient.core.metadata.schema.OType#convert()

The following examples show how to use com.orientechnologies.orient.core.metadata.schema.OType#convert() . 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: CustomAttribute.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <V> V getValue(OProperty property, V defaultValue)
{
	String stringValue = property.getCustom(name);
	if(encode) stringValue = decodeCustomValue(stringValue);
	V ret;
	if(OProperty.class.isAssignableFrom(javaClass))
	{
		ret = (V)resolveProperty(property.getOwnerClass(), stringValue);
	}
	else
	{
		ret = (V) OType.convert(stringValue, javaClass);
	}
	return ret!=null?ret:defaultValue;
}
 
Example 2
Source File: CustomAttribute.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <V> V getValue(OClass oClass, V defaultValue, boolean hiearchical)
{
	String stringValue = oClass.getCustom(name);
	if(encode) stringValue = decodeCustomValue(stringValue);
	V ret;
	if(OProperty.class.isAssignableFrom(javaClass))
	{
		ret = (V)resolveProperty(oClass, stringValue);
	}
	else
	{
		ret = (V) OType.convert(stringValue, javaClass);
	}
	if(ret==null && hiearchical) {
		for(OClass superClass : oClass.getSuperClasses()) {
			if((ret=getValue(superClass, null, true))!=null) break;
		}
	}
	return ret!=null?ret:defaultValue;
}
 
Example 3
Source File: OCSVTransformer.java    From orientdb-etl with Apache License 2.0 5 votes vote down vote up
private Object processKnownType(ODocument doc, int i, String fieldName, String fieldStringValue, OType fieldType) {
  Object fieldValue;
  fieldValue = getCellContent(fieldStringValue);
  try {
    fieldValue = OType.convert(fieldValue, fieldType.getDefaultJavaType());
    doc.field(fieldName, fieldValue);
  } catch (Exception e) {
    processor.getStats().incrementErrors();
    log(OETLProcessor.LOG_LEVELS.ERROR, "Error on converting row %d field '%s' (%d), value '%s' (class:%s) to type: %s",
        processor.getExtractor().getProgress(), fieldName, i, fieldValue, fieldValue.getClass().getName(), fieldType);
  }
  return fieldValue;
}
 
Example 4
Source File: OPropertyValueValidator.java    From wicket-orientdb with Apache License 2.0 4 votes vote down vote up
protected void validateType(final IValidatable<T> validatable,
		final OProperty p, final Object value) {
	if (value != null)
		if (OType.convert(value, p.getLinkedType().getDefaultJavaType()) == null)
			validatable.error(newValidationError("wrongtype", "expectedType", p.getLinkedType().toString()));
}
 
Example 5
Source File: OAbstractLookupTransformer.java    From orientdb-etl with Apache License 2.0 4 votes vote down vote up
protected Object lookup(Object joinValue, final boolean iReturnRIDS) {
  Object result = null;

  if (joinValue != null) {
    if (sqlQuery == null && index == null) {
      // ONLY THE FIRST TIME
      if (lookup.toUpperCase().startsWith("SELECT"))
        sqlQuery = new OSQLSynchQuery<ODocument>(lookup);
      else {
        index = pipeline.getDocumentDatabase().getMetadata().getIndexManager().getIndex(lookup);
        if (index == null) {
          log(OETLProcessor.LOG_LEVELS.DEBUG, "WARNING: index %s not found. Lookups could be really slow", lookup);
          final String[] parts = lookup.split("\\.");
          sqlQuery = new OSQLSynchQuery<ODocument>("SELECT FROM " + parts[0] + " WHERE " + parts[1] + " = ?");
        }
      }
    }

    if (index != null) {
      final OType idxFieldType = index.getDefinition().getTypes()[0];
      joinValue = OType.convert(joinValue, idxFieldType.getDefaultJavaType());
      result = index.get(joinValue);
    } else {
      if (sqlQuery instanceof OSQLSynchQuery)
        ((OSQLSynchQuery) sqlQuery).resetPagination();

      result = pipeline.getDocumentDatabase().query(sqlQuery, joinValue);
    }

    if (result != null && result instanceof Collection) {
      final Collection coll = (Collection) result;

      if (!coll.isEmpty()) {
        if (iReturnRIDS) {
          // CONVERT COLLECTION OF RECORDS IN RIDS
          final List<ORID> resultRIDs = new ArrayList<ORID>(coll.size());
          for (Object o : coll) {
            if (o instanceof OIdentifiable)
              resultRIDs.add(((OIdentifiable) o).getIdentity());
          }
          result = resultRIDs;
        }
      } else
        result = null;
    } else if (result instanceof OIdentifiable) {
      if (iReturnRIDS)
        result = ((OIdentifiable) result).getIdentity();
      else
        result = ((OIdentifiable) result).getRecord();
    }
  }

  return result;
}
 
Example 6
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());
					}
				}
			}
			
		}
	}
}