Java Code Examples for com.orientechnologies.orient.core.metadata.schema.OClass#getProperty()

The following examples show how to use com.orientechnologies.orient.core.metadata.schema.OClass#getProperty() . 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: ApplyEditorChangesBehavior.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
private void addPropertiesToOClass(OSchema schema, OClass oClass, List<OArchitectOProperty> properties) {
    for (OArchitectOProperty property : properties) {
        if (!property.isSubClassProperty()) {
            OProperty oProperty = oClass.getProperty(property.getName());
            if (oProperty == null && !property.isExistsInDb()) {
                oProperty = oClass.createProperty(property.getName(), property.getType());
            } else if (oProperty != null && !property.isExistsInDb() && oProperty.getType() != property.getType()) {
                oProperty.setType(property.getType());
            }
            if (!Strings.isNullOrEmpty(property.getLinkedClass())) {
                setLinkedClassForProperty(property, oProperty, schema);
            }
            CustomAttribute.ORDER.setValue(oProperty, property.getOrder());
        }
    }
}
 
Example 2
Source File: OrientDbLoader.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
protected void createProperties(Row row) {
    OClass clazz;
    if (className != null) {
        clazz = getOrCreateClass(className);
    } else {
        clazz = ((ODocument) row.getPayload()).getSchemaClass();
    }

    int count = row.getFieldCount();
    for (int i = 0; i < count; i++) {
        String fieldName = row.getFieldName(i);
        OProperty property = clazz.getProperty(fieldName);
        if (property == null) {
            OType type = OType.getTypeByClass(row.getFieldType(i));
            try {
                clazz.createProperty(fieldName, type);
            } catch (OSchemaException e) {
                log.error(e.getMessage(), e);
            }

            log.debug("Created property '{}' of type '{}'", fieldName, type);
        }
    }
}
 
Example 3
Source File: OClassIntrospector.java    From Orienteer with Apache License 2.0 6 votes vote down vote up
@Override
public OProperty getNameProperty(OClass oClass) {
	if(oClass==null) return null;
	OProperty ret = CustomAttribute.PROP_NAME.getValue(oClass);
	if(ret!=null) return ret;
	ret = oClass.getProperty("name");
	if(ret!=null) return ret;
	for(OProperty p: oClass.properties())
	{
		if(!p.getType().isMultiValue())
		{
			ret = p;
			if(OType.STRING.equals(p.getType())) break;
		}
	}
	return ret;
}
 
Example 4
Source File: LegacyKeyStoreUpgradeServiceTest.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testUpgradeSchema_TypeDoesNotYetExist() throws Exception {
  service.upgradeSchema();
  try (ODatabaseDocumentTx db = database.getInstance().connect()) {
    OSchema schema = db.getMetadata().getSchema();
    OClass type = schema.getClass("key_store");
    assertThat(type, is(notNullValue()));
    OProperty prop = type.getProperty("name");
    assertThat(prop, is(notNullValue()));
    assertThat(prop.isMandatory(), is(true));
    assertThat(prop.isNotNull(), is(true));
    assertThat(prop.getType(), is(OType.STRING));
    prop = type.getProperty("bytes");
    assertThat(prop, is(notNullValue()));
    assertThat(prop.isMandatory(), is(true));
    assertThat(prop.isNotNull(), is(true));
    assertThat(prop.getType(), is(OType.BINARY));
    assertThat(type.getInvolvedIndexes("name"), hasSize(1));
    assertThat(type.getInvolvedIndexes("name").iterator().next().getType(), is("UNIQUE"));
  }
}
 
Example 5
Source File: RenameFromFieldExtension.java    From guice-persist-orient with MIT License 6 votes vote down vote up
@Override
public void beforeRegistration(final ODatabaseObject db, final SchemeDescriptor descriptor,
                               final Field field, final RenamePropertyFrom annotation) {
    final String oldName = Strings.emptyToNull(annotation.value().trim());
    final String name = field.getName();
    check(oldName != null, "Old name not specified");
    check(!oldName.equals(name), "Defined old name is the same as current property name: %s", name);
    // if class not registered no need  to rename
    if (!descriptor.initialRegistration) {
        final String modelName = descriptor.schemeClass;
        final OClass scheme = db.getMetadata().getSchema().getClass(modelName);
        // old property not exist - no need to rename
        if (scheme.getProperty(oldName) != null) {
            check(scheme.getProperty(name) == null,
                    "Model property %s.%s already exist and can't be renamed from %s",
                    modelName, name, oldName);
            SchemeUtils.command(db, "alter property %s.%s name %s", modelName, oldName, name);
            logger.debug("Scheme property {}.{} renamed from {}", modelName, name, oldName);
        }
    }
}
 
Example 6
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 7
Source File: OLuceneIndexManagerAbstract.java    From orientdb-lucene with Apache License 2.0 5 votes vote down vote up
private void checkCollectionIndex(OIndexDefinition indexDefinition) {

    List<String> fields = indexDefinition.getFields();

    OClass aClass = getDatabase().getMetadata().getSchema().getClass(indexDefinition.getClassName());
    for (String field : fields) {
      OProperty property = aClass.getProperty(field);

      if (property.getType().isEmbedded() && property.getLinkedType() != null) {
        collectionFields.put(field, true);
      } else {
        collectionFields.put(field, false);
      }
    }
  }
 
Example 8
Source File: OArchitectClassesUtils.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private static boolean isSubClassProperty(OProperty property, List<OClass> superClasses) {
    boolean isSubClass = false;
    for (OClass oClass : superClasses) {
        isSubClass = oClass.getProperty(property.getName()) != null;
        if (!isSubClass) {
            List<OClass> classes = oClass.getSuperClasses();
            if (classes != null && !classes.isEmpty()) {
                isSubClass = isSubClassProperty(property, classes);
            }
        } else break;
    }
    return isSubClass;
}
 
Example 9
Source File: OPropertyFullNameConverter.java    From wicket-orientdb with Apache License 2.0 5 votes vote down vote up
@Override
protected OProperty doBackward(String b) {
	ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
	OSchema schema = db.getMetadata().getSchema();
	String className = Strings.beforeFirst(b, '.');
	String propertyName = Strings.afterFirst(b, '.');
	OClass oClass = schema.getClass(className);
	return oClass.getProperty(propertyName);
}
 
Example 10
Source File: ApplyEditorChangesBehavior.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private void setLinkedClassForProperty(OArchitectOProperty architectProperty, OProperty property, OSchema schema) {
    OClass linkedClass = schema.getOrCreateClass(architectProperty.getLinkedClass());
    property.setLinkedClass(linkedClass);
    if (architectProperty.getInverseProperty() != null) {
        OArchitectOProperty p = architectProperty.getInverseProperty();
        if (!Strings.isNullOrEmpty(p.getName()) && p.getType() != null) {
            OProperty inverseProp = linkedClass.getProperty(p.getName());
            if (inverseProp == null) {
                inverseProp = linkedClass.createProperty(p.getName(), p.getType());
            }
            CustomAttribute.PROP_INVERSE.setValue(property, inverseProp);
        }
    }
}
 
Example 11
Source File: CustomAttribute.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
private OProperty resolveProperty(OClass oClass, String propertyName)
{
	if(Strings.isEmpty(propertyName)) return null;
	int indx = propertyName.indexOf('.');
	if(indx>0)
	{
		String className = propertyName.substring(0, indx);
		propertyName = propertyName.substring(indx+1);
		oClass = ((OClassImpl)oClass).getOwner().getClass(className);
		if(oClass==null) return null;
	}
	return oClass.getProperty(propertyName);
}
 
Example 12
Source File: OrientQuartzSchema.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static OProperty maybeCreateProperty(final OClass clazz,
                                             final String name,
                                             final OType type,
                                             final boolean notNull)
{
  OProperty property = clazz.getProperty(name);
  if (property == null) {
    property = clazz.createProperty(name, type);
    if (notNull) {
      property = property.setMandatory(true).setNotNull(true);
    }
  }
  return property;
}
 
Example 13
Source File: OSchemaHelper.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
public OSchemaHelper setupRelationship(String class1Name, String property1Name, String class2Name, String property2Name)
{
	OClass class1 = schema.getClass(class1Name);
	OProperty property1 = class1.getProperty(property1Name);
	OClass class2 = schema.getClass(class2Name);
	OProperty property2 = class2.getProperty(property2Name);
	if(!Objects.equals(property1.getLinkedClass(), class2)) property1.setLinkedClass(class2);
	if(!Objects.equals(property2.getLinkedClass(), class1)) property2.setLinkedClass(class1);
	CustomAttribute.PROP_INVERSE.setValue(property1, property2);
	CustomAttribute.PROP_INVERSE.setValue(property2, property1);
	return this;
}
 
Example 14
Source File: OSchemaHelper.java    From Orienteer with Apache License 2.0 5 votes vote down vote up
public OSchemaHelper setupRelationship(String class1Name, String propertyName, String class2Name) {
	OClass class1 = schema.getClass(class1Name);
	OProperty property = class1.getProperty(propertyName);
	OClass class2 = schema.getClass(class2Name);

	if (!Objects.equals(property.getLinkedClass(), class2)) property.setLinkedClass(class2);

	return this;
}
 
Example 15
Source File: ComponentDatabaseUpgrade_1_5.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void modifyCaseInsensitiveNameField(final OClass type) {
  log.info("Modifying case-insensitive name field on component");
  OProperty ciNameProperty = type.getProperty(P_CI_NAME);
  if (!ciNameProperty.isMandatory()) {
    ciNameProperty.setMandatory(true);
  }
  if (!ciNameProperty.isNotNull()) {
    ciNameProperty.setNotNull(true);
  }
}
 
Example 16
Source File: OPropertyModel.java    From wicket-orientdb with Apache License 2.0 4 votes vote down vote up
@Override
protected OProperty loadInstance() {
		OClass oClass = classModel!=null?classModel.getObject():null;
		return oClass!=null && propertyName!=null?oClass.getProperty(propertyName):null;
}
 
Example 17
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());
					}
				}
			}
			
		}
	}
}
 
Example 18
Source File: AbstractSchemaLocalizationWidget.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
public AbstractSchemaLocalizationWidget(String id, IModel<T> model, IModel<ODocument> widgetDocumentModel) {
    super(id, model, widgetDocumentModel);
    OClass oLocalizationClass = getDatabase().getMetadata().getSchema().getClass(OrienteerLocalizationModule.OCLASS_LOCALIZATION);

    final OQueryDataProvider<ODocument> provider = new OQueryDataProvider<ODocument>("select from OLocalization where key = :key");
    provider.setParameter("key", Model.of(getLocalizationKey(getModelObject())));


    List<IColumn<ODocument, String>> columns = new ArrayList<IColumn<ODocument,String>>();
    columns.add(new OPropertyValueColumn(oLocalizationClass.getProperty(OrienteerLocalizationModule.OPROPERTY_VALUE), getModeModel()));
    OProperty langProperty = oLocalizationClass.getProperty(OrienteerLocalizationModule.OPROPERTY_LANG);
    columns.add(new OPropertyValueComboBoxColumn<String>(langProperty, LanguagesChoiceProvider.INSTANCE, getModeModel()));
    columns.add(new DeleteRowCommandColumn(getModeModel()));
    GenericTablePanel<ODocument> tablePanel = new GenericTablePanel<ODocument>("localizations", columns, provider, 20);
    table = tablePanel.getDataTable();

    table.addCommand(new EditODocumentsCommand(table, getModeModel(), new OClassModel(OrienteerLocalizationModule.OCLASS_LOCALIZATION)));
    table.addCommand(new SaveOLocalizationsCommand(table, getModeModel()));
    table.setCaptionModel(new ResourceModel("class.localization"));

    ajaxFormCommand = new AjaxCommand<ODocument>("add", "command.add") {
    	{
    		OSecurityHelper.secureComponent(this, OSecurityHelper.requireOClass(OrienteerLocalizationModule.OCLASS_LOCALIZATION, OrientPermission.CREATE));
    	}
        @Override
        public void onClick(Optional<AjaxRequestTarget> targetOptional) {
            ODocument newLocalization = new ODocument(OrienteerLocalizationModule.OCLASS_LOCALIZATION);
            T schemaObject = AbstractSchemaLocalizationWidget.this.getModelObject();
            newLocalization.field(OrienteerLocalizationModule.OPROPERTY_KEY, getLocalizationKey(schemaObject));
            newLocalization.field(OrienteerLocalizationModule.OPROPERTY_LANG, "");
            newLocalization.field(OrienteerLocalizationModule.OPROPERTY_STYLE, "");
            newLocalization.field(OrienteerLocalizationModule.OPROPERTY_VARIATION, "");
            newLocalization.field(OrienteerLocalizationModule.OPROPERTY_VALUE, "");
            newLocalization.field(OrienteerLocalizationModule.OPROPERTY_ACTIVE, false);
            getDatabase().save(newLocalization);
            targetOptional.ifPresent(target -> target.add(table));
        }

        @Override
        protected void onConfigure() {
            super.onConfigure();
            setVisibilityAllowed(getModeModel().getObject().equals(DisplayMode.EDIT));
        }

        @Override
        public void onEvent(IEvent<?> event) {
            super.onEvent(event);

            Object payload = event.getPayload();
            if(payload instanceof ActionPerformedEvent) {
                ajaxFormCommand.setVisibilityAllowed(getModeModel().getObject().equals(DisplayMode.EDIT));
                ((ActionPerformedEvent<?>) payload).getTarget().ifPresent(target -> target.add(ajaxFormCommand));
            } else if (payload instanceof AjaxRequestHandler) {
                ((AjaxRequestHandler) payload).add(ajaxFormCommand);
            }
        }
    };

    table.addCommand(ajaxFormCommand.setBootstrapType(BootstrapType.PRIMARY)
            .setIcon(FAIconType.language));
    add(tablePanel);
}
 
Example 19
Source File: DAOTest.java    From Orienteer with Apache License 2.0 4 votes vote down vote up
private OProperty assertProperty(OClass oClass, String property, OType oType) {
	OProperty prop = oClass.getProperty(property);
	assertNotNull("Property was not found", prop);
	assertEquals(oType, prop.getType());
	return prop;
}