/** * {@inheritDoc} * * @see * org.komodo.spi.repository.ValidationManager#addChildRelationshipValidationRule(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String, java.lang.String, java.lang.String, java.util.List, java.util.List, * java.util.List, java.util.List, java.util.List, java.util.List) */ @Override public Rule addChildRelationshipValidationRule( final UnitOfWork transaction, final String name, final String nodeType, final String childType, final List<String> propsThatMustExist, final List<String> propsThatMustNotExist, final List<String> childTypesThatMustExist, final List<String> childTypesThatMustNotExist, final List<LocalizedMessage> descriptions, final List<LocalizedMessage> messages) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(childType, "childType"); // $NON-NLS-1$ ArgCheck.isTrue( ((propsThatMustExist != null) && !propsThatMustExist.isEmpty()) || ((propsThatMustNotExist != null) && !propsThatMustNotExist.isEmpty()) || ((childTypesThatMustExist != null) && !childTypesThatMustExist.isEmpty()) || ((childTypesThatMustNotExist != null) && !childTypesThatMustNotExist.isEmpty()), "at least one relationship collection must not be empty"); //$NON-NLS-1$ if (LOGGER.isDebugEnabled()) { LOGGER.debug( "addChildRelationshipValidationRule: transaction = {0}, name = {1}", transaction.getName(), name); // $NON-NLS-1$ } try { final RuleImpl rule = createRule( transaction, name, KomodoLexicon.Rule.RELATIONSHIP_RULE, Rule.ValidationType.CHILD, Rule.RuleType.RELATIONSHIP, nodeType, descriptions, messages); processMultiValuedProperty( transaction, rule, KomodoLexicon.Rule.PROP_EXISTS, propsThatMustExist); processMultiValuedProperty( transaction, rule, KomodoLexicon.Rule.PROP_ABSENT, propsThatMustNotExist); processMultiValuedProperty( transaction, rule, KomodoLexicon.Rule.CHILD_EXISTS, childTypesThatMustExist); processMultiValuedProperty( transaction, rule, KomodoLexicon.Rule.CHILD_ABSENT, childTypesThatMustNotExist); return rule; } catch (final Exception e) { throw ObjectImpl.handleError(e); } }
/** * {@inheritDoc} * * @see * org.komodo.spi.repository.ValidationManager#addPropertyValueNumberValidationRule(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String, java.lang.String, java.lang.String, java.lang.Number, boolean, * java.lang.Number, boolean, java.util.List, java.util.List) */ @Override public Rule addPropertyValueNumberValidationRule( final UnitOfWork transaction, final String name, final String nodeType, final String propertyName, final Number minValue, final boolean minInclusive, final Number maxValue, final boolean maxInclusive, final List<LocalizedMessage> descriptions, final List<LocalizedMessage> messages) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(propertyName, "propertyName"); // $NON-NLS-1$ ArgCheck.isTrue( (minValue != null) || (maxValue != null), "minValue or maxValue must not be null"); //$NON-NLS-1$ if (LOGGER.isDebugEnabled()) { LOGGER.debug( "addPropertyValueNumberValidationRule: transaction = {0}, name = {1}", transaction.getName(), name); // $NON-NLS-1$ } try { final RuleImpl rule = createRule( transaction, name, KomodoLexicon.Rule.NUMBER_RULE, Rule.ValidationType.PROPERTY, Rule.RuleType.NUMBER, nodeType, descriptions, messages); rule.setProperty(transaction, KomodoLexicon.Rule.JCR_NAME, propertyName); if (minValue != null) { rule.setProperty(transaction, KomodoLexicon.Rule.MIN_VALUE, minValue.toString()); rule.setProperty(transaction, KomodoLexicon.Rule.MIN_VALUE_INCLUSIVE, minInclusive); } if (maxValue != null) { rule.setProperty(transaction, KomodoLexicon.Rule.MAX_VALUE, maxValue.toString()); rule.setProperty(transaction, KomodoLexicon.Rule.MAX_VALUE_INCLUSIVE, maxInclusive); } return rule; } catch (final Exception e) { throw ObjectImpl.handleError(e); } }
@Before public void basicInit() throws KException { transaction = Mockito.mock(UnitOfWork.class); Mockito.when(transaction.getState()).thenReturn(State.NOT_STARTED); UnitOfWork uow = Mockito.mock(UnitOfWork.class); Mockito.when(uow.getState()).thenReturn(State.NOT_STARTED); repository = Mockito.mock(Repository.class); UnitOfWorkListener listener = Matchers.any(); Mockito.when( repository.createTransaction( Matchers.anyString(), Matchers.anyString(), Matchers.anyBoolean(), listener)) .thenReturn(uow); }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.Permission#getConditions(org.komodo.spi.repository.Repository.UnitOfWork) */ @Override public Condition[] getConditions(final UnitOfWork transaction) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ Condition[] result = null; if (hasChild( transaction, VdbLexicon.DataRole.Permission.CONDITIONS, VdbLexicon.DataRole.Permission.CONDITIONS)) { final KomodoObject grouping = getChild( transaction, VdbLexicon.DataRole.Permission.CONDITIONS, VdbLexicon.DataRole.Permission.CONDITIONS); final List<Condition> temp = new ArrayList<>(); for (final KomodoObject kobject : grouping.getChildrenOfType( transaction, VdbLexicon.DataRole.Permission.Condition.CONDITION)) { final Condition condition = new ConditionImpl(transaction, getRepository(), kobject.getAbsolutePath()); temp.add(condition); } result = temp.toArray(new Condition[temp.size()]); } else { result = Condition.NO_CONDITIONS; } return result; }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.DataRole#getMappedRoles(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String[]) */ @Override public String[] getMappedRoles(final UnitOfWork transaction, final String... namePatterns) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ final Property property = getProperty(transaction, VdbLexicon.DataRole.MAPPED_ROLE_NAMES); if (property == null) { return StringConstants.EMPTY_ARRAY; } final boolean matchPattern = ((namePatterns != null) && (namePatterns.length != 0)); final List<String> roleNames = new ArrayList<>(); for (final String value : property.getStringValues(transaction)) { if (matchPattern) { for (final String pattern : namePatterns) { // convert pattern to a regex final String regex = pattern.replace("*", ".*"); // $NON-NLS-1$ //$NON-NLS-2$ if (value.matches(regex)) { roleNames.add(value); } } } else { roleNames.add(value); } } return roleNames.toArray(new String[roleNames.size()]); }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.Permission#getMasks(org.komodo.spi.repository.Repository.UnitOfWork) */ @Override public Mask[] getMasks(final UnitOfWork transaction) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ Mask[] result = null; if (hasChild( transaction, VdbLexicon.DataRole.Permission.MASKS, VdbLexicon.DataRole.Permission.MASKS)) { final KomodoObject grouping = getChild( transaction, VdbLexicon.DataRole.Permission.MASKS, VdbLexicon.DataRole.Permission.MASKS); final List<Mask> temp = new ArrayList<>(); for (final KomodoObject kobject : grouping.getChildrenOfType(transaction, VdbLexicon.DataRole.Permission.Mask.MASK)) { final Mask mask = new MaskImpl(transaction, getRepository(), kobject.getAbsolutePath()); temp.add(mask); } result = temp.toArray(new Mask[temp.size()]); } else { result = Mask.NO_MASKS; } return result; }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.Permission#removeCondition(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String) */ @Override public void removeCondition(final UnitOfWork transaction, final String conditionToRemove) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(conditionToRemove, "conditionToRemove"); // $NON-NLS-1$ boolean found = false; final Condition[] conditions = getConditions(transaction); if (conditions.length != 0) { for (final Condition condition : conditions) { if (conditionToRemove.equals(condition.getName(transaction))) { condition.remove(transaction); found = true; break; } } } if (!found) { throw new KException( Messages.getString(Relational.CONDITION_NOT_FOUND_TO_REMOVE, conditionToRemove)); } }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.Permission#removeMask(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String) */ @Override public void removeMask(final UnitOfWork transaction, final String maskToRemove) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(maskToRemove, "maskToRemove"); // $NON-NLS-1$ boolean found = false; final Mask[] masks = getMasks(transaction); if (masks.length != 0) { for (final Mask mask : masks) { if (maskToRemove.equals(mask.getName(transaction))) { mask.remove(transaction); found = true; break; } } } if (!found) { throw new KException(Messages.getString(Relational.MASK_NOT_FOUND_TO_REMOVE, maskToRemove)); } }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.DataRole#addMappedRole(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String) */ @Override public String[] addMappedRole(final UnitOfWork transaction, final String roleNameToAdd) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(roleNameToAdd, "roleNameToAdd"); // $NON-NLS-1$ String[] result = null; final String[] current = getMappedRoles(transaction); int i = 0; if (current.length == 0) { // this is first mapped role name result = new String[1]; } else { // add to existing (make sure it doesn't already exist) result = new String[current.length + 1]; for (final String mappedRoleName : current) { if (mappedRoleName.equals(roleNameToAdd)) { throw new KException(Messages.getString(Relational.DUPLICATE_ROLE_NAME, roleNameToAdd)); } result[i++] = mappedRoleName; } } result[i] = roleNameToAdd; setProperty(transaction, VdbLexicon.DataRole.MAPPED_ROLE_NAMES, (Object[]) result); return result; }
/** * {@inheritDoc} * * @see * org.komodo.relational.internal.RelationalObjectImpl#getChildren(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String[]) */ @Override public KomodoObject[] getChildren(final UnitOfWork transaction, final String... namePatterns) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ final KomodoObject[] result = getPermissions(transaction, namePatterns); return result; }
/** * {@inheritDoc} * * @see * org.komodo.relational.internal.RelationalObjectImpl#getParent(org.komodo.spi.repository.Repository.UnitOfWork) */ @Override public Vdb getParent(final UnitOfWork transaction) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state must be NOT_STARTED"); //$NON-NLS-1$ final KomodoObject grouping = super.getParent(transaction); final Vdb result = Vdb.RESOLVER.resolve(transaction, grouping.getParent(transaction)); return result; }
/** * {@inheritDoc} * * @see * org.komodo.spi.repository.ValidationManager#addPropertyPatternRule(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.util.List, * java.util.List) */ @Override public Rule addPropertyPatternRule( final UnitOfWork transaction, final String name, final String nodeType, final String propertyName, final String pattern, final List<LocalizedMessage> descriptions, final List<LocalizedMessage> messages) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(propertyName, "propertyName"); // $NON-NLS-1$ ArgCheck.isNotEmpty(pattern, "pattern"); // $NON-NLS-1$ if (LOGGER.isDebugEnabled()) { LOGGER.debug( "addPropertyPatternRule: transaction = {0}, name = {1}", transaction.getName(), name); // $NON-NLS-1$ } try { final RuleImpl rule = createRule( transaction, name, KomodoLexicon.Rule.PATTERN_RULE, Rule.ValidationType.PROPERTY, Rule.RuleType.PATTERN, nodeType, descriptions, messages); rule.setProperty(transaction, KomodoLexicon.Rule.JCR_NAME, propertyName); rule.setProperty(transaction, KomodoLexicon.Rule.PATTERN, pattern); return rule; } catch (final Exception e) { throw ObjectImpl.handleError(e); } }
private void commitImport(final String source, ImportMessages importMessages) throws Exception { UnitOfWork trans = getTransaction(); final String txName = trans.getName(); trans.commit(); Repository.UnitOfWorkListener uowListener = trans.getCallback(); SynchronousCallback callback = null; if (uowListener != null && uowListener instanceof SynchronousCallback) { callback = (SynchronousCallback) uowListener; } final boolean success = callback.await(3, TimeUnit.MINUTES); if (success) { // For imports, if has callback error - add to import errors and return. if (callback.hasError()) { importMessages.addErrorMessage(callback.error()); return; } final KException error = trans.getError(); final Repository.UnitOfWork.State txState = trans.getState(); if ((error != null) || !State.COMMITTED.equals(txState)) { throw new KException(I18n.bind(ShellI18n.transactionCommitError, txName), error); } } else { throw new KException(I18n.bind(ShellI18n.transactionTimeout, txName)); } }
/** * {@inheritDoc} * * @see * org.komodo.spi.repository.ValidationManager#addSameNameSiblingValidationRule(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String, java.lang.String, boolean, java.util.List, java.util.List) */ @Override public Rule addSameNameSiblingValidationRule( final UnitOfWork transaction, final String name, final String nodeType, final boolean matchType, final List<LocalizedMessage> descriptions, final List<LocalizedMessage> messages) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ if (LOGGER.isDebugEnabled()) { LOGGER.debug( "addSameNameSiblingValidationRule: transaction = {0}, name = {1}", transaction.getName(), name); // $NON-NLS-1$ } try { final RuleImpl rule = createRule( transaction, name, KomodoLexicon.Rule.SNS_RULE, Rule.ValidationType.CHILD, Rule.RuleType.SAME_NAME_SIBLING, nodeType, descriptions, messages); rule.setProperty(transaction, KomodoLexicon.Rule.MATCH_TYPE, matchType); return rule; } catch (final Exception e) { throw ObjectImpl.handleError(e); } }
/** * {@inheritDoc} * * @see * org.komodo.relational.internal.RelationalObjectImpl#hasChild(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String, java.lang.String) */ @Override public boolean hasChild(final UnitOfWork transaction, final String name, final String typeName) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state must be NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(name, "name"); // $NON-NLS-1$ ArgCheck.isNotEmpty(typeName, "typeName"); // $NON-NLS-1$ if (VdbLexicon.DataRole.Permission.PERMISSION.equals(typeName)) { return (getPermissions(transaction, name).length != 0); } return false; }
/** * {@inheritDoc} * * @see * org.komodo.repository.ObjectImpl#getChildren(org.komodo.spi.repository.Repository.UnitOfWork) */ @Override public KomodoObject[] getChildren(final UnitOfWork transaction) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ final Condition[] conditions = getConditions(transaction); final Mask[] masks = getMasks(transaction); final KomodoObject[] result = new KomodoObject[conditions.length + masks.length]; System.arraycopy(conditions, 0, result, 0, conditions.length); System.arraycopy(masks, 0, result, conditions.length, masks.length); return result; }
/** * {@inheritDoc} * * @see * org.komodo.relational.internal.RelationalObjectImpl#getChild(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String) */ @Override public KomodoObject getChild(final UnitOfWork transaction, final String name) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state must be NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(name, "name"); // $NON-NLS-1$ final KomodoObject[] matches = getPermissions(transaction, name); if (matches.length != 0) { return matches[0]; } // child does not exist throw new KException( Messages.getString( org.komodo.repository.Messages.Komodo.CHILD_NOT_FOUND, name, getAbsolutePath())); }
/** * {@inheritDoc} * * @see * org.komodo.relational.internal.RelationalObjectImpl#getChildrenOfType(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String, java.lang.String[]) */ @Override public KomodoObject[] getChildrenOfType( final UnitOfWork transaction, final String type, final String... namePatterns) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ KomodoObject[] result = null; if (VdbLexicon.DataRole.Permission.PERMISSION.equals(type)) { result = getPermissions(transaction, namePatterns); } else { result = KomodoObject.EMPTY_ARRAY; } return result; }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.DataRole#removePermission(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String) */ @Override public void removePermission(final UnitOfWork transaction, final String permissionToRemove) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(permissionToRemove, "permissionToRemove"); // $NON-NLS-1$ final Permission[] permissions = getPermissions(transaction, permissionToRemove); if (permissions.length == 0) { throw new KException( Messages.getString(Relational.PERMISSION_NOT_FOUND_TO_REMOVE, permissionToRemove)); } // remove first occurrence permissions[0].remove(transaction); }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.DataRole#removeMappedRole(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String) */ @Override public String[] removeMappedRole(final UnitOfWork transaction, final String roleNameToRemove) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ ArgCheck.isNotEmpty(roleNameToRemove, "roleNameToRemove"); // $NON-NLS-1$ final String[] current = getMappedRoles(transaction); if (current.length == 0) { throw new KException( Messages.getString(Relational.MAPPED_ROLE_NOT_FOUND_TO_REMOVE, roleNameToRemove)); } final String[] result = new String[current.length - 1]; boolean found = false; int i = 0; for (final String mappedRoleName : current) { if (mappedRoleName.equals(roleNameToRemove)) { found = true; } else { result[i++] = mappedRoleName; } } if (!found) { throw new KException( Messages.getString(Relational.MAPPED_ROLE_NOT_FOUND_TO_REMOVE, roleNameToRemove)); } final Object[] newValue = ((result.length == 0) ? null : result); setProperty(transaction, VdbLexicon.DataRole.MAPPED_ROLE_NAMES, newValue); return result; }
/** * {@inheritDoc} * * @see * org.komodo.relational.vdb.DataRole#getPermissions(org.komodo.spi.repository.Repository.UnitOfWork, * java.lang.String[]) */ @Override public Permission[] getPermissions(final UnitOfWork transaction, final String... namePatterns) throws KException { ArgCheck.isNotNull(transaction, "transaction"); // $NON-NLS-1$ ArgCheck.isTrue( (transaction.getState() == State.NOT_STARTED), "transaction state is not NOT_STARTED"); //$NON-NLS-1$ final KomodoObject grouping = getPermissionsGroupingNode(transaction); if (grouping != null) { final List<Permission> temp = new ArrayList<>(); for (final KomodoObject kobject : grouping.getChildren(transaction, namePatterns)) { final Permission permission = new PermissionImpl(transaction, getRepository(), kobject.getAbsolutePath()); temp.add(permission); } return temp.toArray(new Permission[temp.size()]); } return Permission.NO_PERMISSIONS; }