private static Boolean convertBool(Attribute attr, Object paramValue) { Boolean value; if (paramValue != null) { if (paramValue instanceof String) { value = Boolean.valueOf((String) paramValue); } else if (paramValue instanceof Boolean) { value = (Boolean) paramValue; } else { throw new MolgenisDataException( format( "Attribute [%s] value is of type [%s] instead of [%s] or [%s]", attr.getName(), paramValue.getClass().getSimpleName(), String.class.getSimpleName(), Boolean.class.getSimpleName())); } } else { // boolean false is not posted (http feature), so if null and required, should be false value = !attr.isNillable() ? false : null; } return value; }
private void initRequiredValueValidation(ValidationResource validationResource) { if (!getCapabilities().contains(VALIDATE_NOTNULL_CONSTRAINT)) { List<Attribute> requiredValueAttrs = stream(getEntityType().getAtomicAttributes().spliterator(), false) .filter(attr -> !attr.isNillable() && attr.getExpression() == null) .collect(toList()); validationResource.setRequiredValueAttrs(requiredValueAttrs); } }
/** * Generates column constraint SQL, e.g. 'NOT NULL DEFAULT 123' * * @param entityType entity type * @param attr attribute * @param columnConstraintsMode whether or not to add default constraint to generated SQL * @return column constraint SQL */ private static String getSqlColumnConstraints( EntityType entityType, Attribute attr, ColumnMode columnConstraintsMode) { StringBuilder sqlBuilder = new StringBuilder(); if (!attr.getName().equals(entityType.getIdAttribute().getName())) { if (!attr.isNillable()) { sqlBuilder.append("NOT NULL"); } } if (columnConstraintsMode == INCLUDE_DEFAULT_CONSTRAINT && generateSqlColumnDefaultConstraint(attr)) { if (sqlBuilder.length() > 0) { sqlBuilder.append(' '); } sqlBuilder.append("DEFAULT ").append(getSqlDefaulValue(attr)); } return sqlBuilder.toString(); }
private static Map<String, Object> attributeToMap(Attribute attribute) { Map<String, Object> map = new HashMap<>(); map.put(AttributeMetadata.NAME, attribute.getName()); map.put(AttributeMetadata.LABEL, attribute.getLabel()); map.put(AttributeMetadata.DESCRIPTION, attribute.getDescription()); map.put(AttributeMetadata.TYPE, attribute.getDataType().toString()); map.put(AttributeMetadata.IS_NULLABLE, attribute.isNillable()); map.put(AttributeMetadata.IS_UNIQUE, attribute.isUnique()); if (attribute.getRefEntity() != null) { map.put(AttributeMetadata.REF_ENTITY_TYPE, attribute.getRefEntity().getId()); } return map; } }
private void updateNillable(EntityType entityType, Attribute attr, Attribute updatedAttr) { if (attr.isNillable() && !updatedAttr.isNillable()) { String sqlSetNotNull = getSqlSetNotNull(entityType, updatedAttr); if (LOG.isDebugEnabled()) { } else if (!attr.isNillable() && updatedAttr.isNillable()) { Attribute idAttr = entityType.getIdAttribute(); if (idAttr != null && idAttr.getName().equals(attr.getName())) {
private static Map<String, Object> attributeToMap(Attribute attribute) { Map<String, Object> map = new HashMap<String, Object>(); map.put(AttributeMetadata.NAME, attribute.getName()); map.put(AttributeMetadata.LABEL, attribute.getLabel()); map.put(AttributeMetadata.DESCRIPTION, attribute.getDescription()); map.put(AttributeMetadata.TYPE, attribute.getDataType().toString()); map.put(AttributeMetadata.IS_NULLABLE, attribute.isNillable()); map.put(AttributeMetadata.IS_UNIQUE, attribute.isUnique()); if (attribute.getRefEntity() != null) { map.put(AttributeMetadata.REF_ENTITY_TYPE, attribute.getRefEntity().getId()); } return map; } }
private static Map<String, List<Map<String, Object>>> createMrefMap( Attribute idAttr, List<Attribute> junctionTableAttrs, List<? extends Entity> entitiesBatch) { Map<String, List<Map<String, Object>>> mrefs = Maps.newHashMapWithExpectedSize(junctionTableAttrs.size()); AtomicInteger seqNr = new AtomicInteger(); for (Entity entity : entitiesBatch) { for (Attribute attr : junctionTableAttrs) { Iterable<Entity> refEntities = entity.getEntities(attr.getName()); // Not-Null constraint doesn't exist for MREF attributes since they are stored in junction // tables, // so validate manually. if (!attr.isNillable() && Iterables.isEmpty(refEntities)) { throw new MolgenisValidationException( new ConstraintViolation( format( "The attribute [%s] of entity [%s] with id [%s] can not be null.", attr.getName(), attr.getEntity().getId(), entity.getIdValue().toString()))); } mrefs.putIfAbsent(attr.getName(), new ArrayList<>()); seqNr.set(0); for (Entity val : refEntities) { Map<String, Object> mref = createJunctionTableRowData(seqNr.getAndIncrement(), idAttr, val, attr, entity); mrefs.get(attr.getName()).add(mref); } } } return mrefs; }
this.nillable = attr.isNillable(); } else this.nillable = null;
/** * Add attribute to entityType. * * @param entityType the {@link EntityType} to add attribute to * @param attr attribute to add */ private void addAttributeInternal(EntityType entityType, Attribute attr) { if (!isPersisted(attr)) { return; } if (isMultipleReferenceType(attr)) { createJunctionTable(entityType, attr); if (attr.getDefaultValue() != null && !attr.isNillable()) { @SuppressWarnings("unchecked") Iterable<Entity> defaultRefEntities = (Iterable<Entity>) AttributeUtils.getDefaultTypedValue(attr); if (!Iterables.isEmpty(defaultRefEntities)) { createJunctionTableRows(entityType, attr, defaultRefEntities); } } } else { createColumn(entityType, attr); } }
if (!attr.isNillable() && mrefs.isEmpty()) { throw new MolgenisValidationException( new ConstraintViolation(
if (ownAttr.isNillable()) { throw new MolgenisValidationException( new ConstraintViolation(
if (!Objects.equals(attr.isNillable(), updatedAttr.isNillable())) { updateNillable(entityType, attr, updatedAttr);
if (ownIdAttr.isNillable()) { throw new MolgenisValidationException( new ConstraintViolation(
this.nillable = attr.isNillable(); this.readOnly = attr.isReadOnly(); this.defaultValue = attr.getDefaultValue();
EditorSort orderBy = sortMapper.toEditorSort(attribute.getOrderBy()); String expression = attribute.getExpression(); boolean nullable = attribute.isNillable(); boolean auto = attribute.isAuto(); boolean visible = attribute.isVisible();