use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.
the class InstanceContextTester method testAllowAddIndex.
/**
* Tests if for the given entity definition a new index context may be
* created.
*
* @param entityDef the entity definition
* @return if adding an index context is allowed
*/
private boolean testAllowAddIndex(EntityDefinition entityDef) {
if (entityDef.getSchemaSpace().equals(SchemaSpaceID.TARGET)) {
// only allowed on source properties
return false;
}
// XXX for now only a simple test based on type and cardinality
List<ChildContext> path = entityDef.getPropertyPath();
if (path != null && !path.isEmpty()) {
ChildContext lastContext = path.get(path.size() - 1);
ChildDefinition<?> lastDef = lastContext.getChild();
// test type & cardinality
Cardinality cardinality;
try {
cardinality = DefinitionUtil.getCardinality(lastDef);
} catch (Exception e) {
return false;
}
if (cardinality.getMaxOccurs() == Cardinality.UNBOUNDED) {
return true;
}
return cardinality.getMaxOccurs() > 1;
}
return false;
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.
the class InstanceContextTester method testAllowAddNamed.
/**
* Tests if for the given entity definition a new instance context may be
* created.
*
* @param entityDef the entity definition
* @return if adding an instance context is allowed
*/
private boolean testAllowAddNamed(EntityDefinition entityDef) {
if (entityDef.getSchemaSpace().equals(SchemaSpaceID.SOURCE)) {
// only allowed on target properties
return false;
}
// XXX for now only a simple test based on type and cardinality
List<ChildContext> path = entityDef.getPropertyPath();
if (path != null && !path.isEmpty()) {
ChildContext lastContext = path.get(path.size() - 1);
ChildDefinition<?> lastDef = lastContext.getChild();
// test type & cardinality
Cardinality cardinality;
try {
cardinality = DefinitionUtil.getCardinality(lastDef);
} catch (Exception e) {
return false;
}
if (cardinality.getMaxOccurs() == Cardinality.UNBOUNDED) {
return true;
}
return cardinality.getMaxOccurs() > 1;
}
return false;
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.
the class StyledDefinitionLabelProvider method update.
/**
* @see StyledCellLabelProvider#update(ViewerCell)
*/
@Override
public void update(ViewerCell cell) {
Object element = cell.getElement();
element = extractElement(element);
StyledString text = new StyledString(defaultLabels.getText(element));
cell.setImage(defaultLabels.getImage(element));
String contextText = null;
String countText = null;
if (element instanceof EntityDefinition) {
PopulationService ps = PlatformUI.getWorkbench().getService(PopulationService.class);
if (ps != null) {
Population pop = ps.getPopulation((EntityDefinition) element);
int count = pop.getOverallCount();
int parents = pop.getParentsCount();
switch(count) {
case Population.UNKNOWN:
countText = "\u00d7?";
break;
case 0:
break;
default:
countText = "\u00d7" + count;
if (parents != count) {
countText += " (" + parents + ")";
}
}
}
contextText = AlignmentUtil.getContextText((EntityDefinition) element);
element = ((EntityDefinition) element).getDefinition();
}
// append cardinality
if (!suppressCardinality && element instanceof ChildDefinition<?>) {
Cardinality cardinality = null;
if (((ChildDefinition<?>) element).asGroup() != null) {
cardinality = ((ChildDefinition<?>) element).asGroup().getConstraint(Cardinality.class);
} else if (((ChildDefinition<?>) element).asProperty() != null) {
cardinality = ((ChildDefinition<?>) element).asProperty().getConstraint(Cardinality.class);
}
if (cardinality != null) {
// only append cardinality if it isn't 1/1
if (cardinality.getMinOccurs() != 1 || cardinality.getMaxOccurs() != 1) {
String card = " " + MessageFormat.format("({0}..{1})", new Object[] { Long.valueOf(cardinality.getMinOccurs()), (cardinality.getMaxOccurs() == Cardinality.UNBOUNDED) ? ("n") : (Long.valueOf(cardinality.getMaxOccurs())) });
text.append(card, StyledString.COUNTER_STYLER);
}
}
}
if (contextText != null) {
contextText = " " + contextText;
text.append(contextText, StyledString.DECORATIONS_STYLER);
}
if (countText != null) {
countText = " " + countText;
text.append(countText, StyledString.QUALIFIER_STYLER);
}
cell.setText(text.toString());
cell.setStyleRanges(text.getStyleRanges());
Color foreground = getForeground(cell.getElement());
cell.setForeground(foreground);
Color background = getBackground(cell.getElement());
cell.setBackground(background);
super.update(cell);
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.
the class GroupUtil method allowAddCheckCardinality.
/**
* Determines if another value of the given property may be added to the
* given group based on the cardinality of the property.
*
* @param group the group
* @param propertyName the property name
* @return if another property value may be added to the group based on the
* property cardinality
*/
private static boolean allowAddCheckCardinality(Group group, QName propertyName) {
ChildDefinition<?> child = group.getDefinition().getChild(propertyName);
Cardinality cardinality = DefinitionUtil.getCardinality(child);
// check maximum
long max = cardinality.getMaxOccurs();
if (max == Cardinality.UNBOUNDED) {
// add allowed in any case
return true;
} else if (max <= 0) {
// add never allowed
return false;
}
Object[] values = group.getProperty(propertyName);
if (values == null) {
// allowed because max is 1 or more
return true;
}
return values.length < max;
}
use of eu.esdihumboldt.hale.common.schema.model.constraint.property.Cardinality in project hale by halestudio.
the class GroupUtil method isValidCardinality.
/**
* Determines if a child is contained in a given group with a valid minimum
* cardinality.
*
* @param group the group
* @param childDef the child definition
* @return if the minimum cardinality of the child definition is matched in
* the group
*/
static boolean isValidCardinality(Group group, ChildDefinition<?> childDef) {
Cardinality cardinality = null;
if (childDef.asProperty() != null) {
cardinality = childDef.asProperty().getConstraint(Cardinality.class);
} else if (childDef.asGroup() != null) {
cardinality = childDef.asGroup().getConstraint(Cardinality.class);
} else {
log.error("Unrecognized child definition.");
}
if (cardinality != null) {
// check minimum
long min = cardinality.getMinOccurs();
if (min > 0 && min != Cardinality.UNBOUNDED) {
Object[] values = group.getProperty(childDef.getName());
int count = (values == null) ? (0) : (values.length);
if (min > count) {
return false;
}
}
}
return true;
}
Aggregations