use of org.alfresco.service.cmr.dictionary.Constraint in project acs-community-packaging by Alfresco.
the class BaseComponentGenerator method setupStringLengthConstraint.
/**
* Sets up a default validation rule for the string length constraint
*
* @param context FacesContext
* @param propertySheet The property sheet to add the validation rule to
* @param property The property being generated
* @param component The component representing the property
* @param constraint The constraint to setup
* @param realTimeChecking true to make the client validate as the user types
*/
protected void setupStringLengthConstraint(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem property, UIComponent component, StringLengthConstraint constraint, boolean realTimeChecking) {
int min = constraint.getMinLength();
int max = constraint.getMaxLength();
List<String> params = new ArrayList<String>(3);
// add the value parameter
String value = "document.getElementById('" + component.getClientId(context) + "')";
params.add(value);
// add the min parameter
params.add(Integer.toString(min));
// add the max parameter
params.add(Integer.toString(max));
// add the validation failed message to show
String msg = Application.getMessage(context, "validation_string_length");
addStringConstraintParam(params, MessageFormat.format(msg, new Object[] { property.getResolvedDisplayLabel(), min, max }));
// add the validation case to the property sheet
propertySheet.addClientValidation(new ClientValidation("validateStringLength", params, realTimeChecking));
}
use of org.alfresco.service.cmr.dictionary.Constraint in project acs-community-packaging by Alfresco.
the class TextFieldGenerator method getListOfValuesConstraint.
/**
* Retrieves the list of values constraint for the item, if it has one
*
* @param context FacesContext
* @param propertySheet The property sheet being generated
* @param item The item being generated
* @return The constraint if the item has one, null otherwise
*/
protected ListOfValuesConstraint getListOfValuesConstraint(FacesContext context, UIPropertySheet propertySheet, PropertySheetItem item) {
ListOfValuesConstraint lovConstraint = null;
// get the property definition for the item
PropertyDefinition propertyDef = getPropertyDefinition(context, propertySheet.getNode(), item.getName());
if (propertyDef != null) {
// go through the constaints and see if it has the
// list of values constraint
List<ConstraintDefinition> constraints = propertyDef.getConstraints();
for (ConstraintDefinition constraintDef : constraints) {
Constraint constraint = constraintDef.getConstraint();
if (constraint instanceof ListOfValuesConstraint) {
lovConstraint = (ListOfValuesConstraint) constraint;
break;
}
}
}
return lovConstraint;
}
use of org.alfresco.service.cmr.dictionary.Constraint in project records-management by Alfresco.
the class RMCaveatConfigComponentImpl method validateAndReset.
/**
* Validate the caveat config and optionally update the cache.
*
* @param nodeRef The nodeRef of the config
* @param updateCache Set to <code>true</code> to update the cache
*/
@SuppressWarnings("unchecked")
protected void validateAndReset(NodeRef nodeRef) {
ContentReader cr = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
if (cr != null) {
// TODO - check who can change caveat config !
// TODO - locking (or checkout/checkin)
String caveatConfigData = cr.getContentString();
if (caveatConfigData != null) {
NodeRef existing = getCaveatConfigNode();
if ((existing != null && (!existing.equals(nodeRef)))) {
throw new AlfrescoRuntimeException("Cannot create more than one caveat config (existing=" + existing + ", new=" + nodeRef + ")");
}
try {
if (logger.isTraceEnabled()) {
logger.trace(caveatConfigData);
}
Set<QName> models = new HashSet<QName>(1);
Set<QName> props = new HashSet<QName>(10);
Set<String> expectedPrefixes = new HashSet<String>(10);
if (caveatModelQNames.size() > 0) {
models.addAll(caveatModelQNames);
} else {
models.addAll(dictionaryService.getAllModels());
}
if (logger.isTraceEnabled()) {
logger.trace("validateAndReset: models to check " + models);
}
for (QName model : models) {
props.addAll(dictionaryService.getProperties(model, DATATYPE_TEXT));
expectedPrefixes.addAll(namespaceService.getPrefixes(model.getNamespaceURI()));
}
if (props.size() == 0) {
logger.warn("validateAndReset: no caveat properties found");
} else {
if (logger.isTraceEnabled()) {
logger.trace("validateAndReset: properties to check " + props);
}
}
Map<String, Object> caveatConfigMap = JSONtoFmModel.convertJSONObjectToMap(caveatConfigData);
for (Map.Entry<String, Object> conEntry : caveatConfigMap.entrySet()) {
String conStr = conEntry.getKey();
QName conQName = QName.resolveToQName(namespaceService, conStr);
// check prefix
String conPrefix = QName.splitPrefixedQName(conStr)[0];
boolean prefixFound = false;
for (String expectedPrefix : expectedPrefixes) {
if (conPrefix.equals(expectedPrefix)) {
prefixFound = true;
}
}
if (!prefixFound) {
throw new AlfrescoRuntimeException("Unexpected prefix: " + conPrefix + " (" + conStr + ") expected one of " + expectedPrefixes + ")");
}
Map<String, List<String>> caveatMap = (Map<String, List<String>>) conEntry.getValue();
List<String> allowedValues = null;
@SuppressWarnings("unused") boolean found = false;
for (QName propertyName : props) {
PropertyDefinition propDef = dictionaryService.getProperty(propertyName);
List<ConstraintDefinition> conDefs = propDef.getConstraints();
for (ConstraintDefinition conDef : conDefs) {
final Constraint con = conDef.getConstraint();
if (con instanceof RMListOfValuesConstraint) {
String conName = ((RMListOfValuesConstraint) con).getShortName();
if (conName.equals(conStr)) {
// note: assumes only one caveat/LOV against a given property
allowedValues = AuthenticationUtil.runAs(new RunAsWork<List<String>>() {
public List<String> doWork() {
return ((RMListOfValuesConstraint) con).getAllowedValues();
}
}, AuthenticationUtil.getSystemUserName());
found = true;
break;
}
}
}
}
if (allowedValues != null) {
if (logger.isInfoEnabled()) {
logger.info("Processing constraint: " + conQName);
}
for (Map.Entry<String, List<String>> caveatEntry : caveatMap.entrySet()) {
String authorityName = caveatEntry.getKey();
List<String> caveatList = caveatEntry.getValue();
// validate authority (user or group) - note: groups are configured with fullname (ie. GROUP_xxx)
if ((!authorityService.authorityExists(authorityName) && !personService.personExists(authorityName))) {
// TODO - review warnings (& I18N)
String msg = "User/group does not exist: " + authorityName + " (constraint=" + conStr + ")";
logger.warn(msg);
}
// validate caveat list
for (String value : caveatList) {
if (!allowedValues.contains(value)) {
// TODO - review warnings (& add I18N)
String msg = "Invalid value in list: " + value + " (authority=" + authorityName + ", constraint=" + conStr + ")";
logger.warn(msg);
}
}
}
}
}
try {
writeLock.lock();
// we can't just clear the cache, as all puts to the cache afterwards in this transaction will be ignored
// first delete all keys that are now not in the config
caveatConfig.getKeys().retainAll(caveatConfigMap.keySet());
for (Map.Entry<String, Object> conEntry : caveatConfigMap.entrySet()) {
String conStr = conEntry.getKey();
Map<String, List<String>> caveatMap = (Map<String, List<String>>) conEntry.getValue();
Map<String, List<String>> cacheValue = caveatConfig.get(conStr);
if (cacheValue == null || !cacheValue.equals(caveatMap)) {
// update the cache
caveatConfig.put(conStr, caveatMap);
}
}
} finally {
writeLock.unlock();
}
} catch (JSONException e) {
throw new AlfrescoRuntimeException("Invalid caveat config syntax: " + e);
}
}
}
}
use of org.alfresco.service.cmr.dictionary.Constraint in project records-management by Alfresco.
the class RMCaveatConfigServiceImpl method getRMConstraint.
/**
* Get an RMConstraintInfo
* @param listQName
* @return the constraint or null if it does not exist
*/
public RMConstraintInfo getRMConstraint(QName listQName) {
ConstraintDefinition dictionaryDef = dictionaryService.getConstraint(listQName);
if (dictionaryDef != null) {
Constraint con = dictionaryDef.getConstraint();
if (con instanceof RMListOfValuesConstraint) {
final RMListOfValuesConstraint def = (RMListOfValuesConstraint) con;
RMConstraintInfo info = new RMConstraintInfo();
info.setName(listQName.toPrefixString());
info.setTitle(con.getTitle());
List<String> allowedValues = AuthenticationUtil.runAs(new RunAsWork<List<String>>() {
public List<String> doWork() {
return def.getAllowedValues();
}
}, AuthenticationUtil.getSystemUserName());
info.setAllowedValues(allowedValues.toArray(new String[allowedValues.size()]));
info.setCaseSensitive(def.isCaseSensitive());
return info;
}
}
return null;
}
use of org.alfresco.service.cmr.dictionary.Constraint in project records-management by Alfresco.
the class RecordsManagementAdminServiceImpl method getCustomConstraintDefinitions.
/**
* @see org.alfresco.module.org_alfresco_module_rm.admin.RecordsManagementAdminService#getCustomConstraintDefinitions(org.alfresco.service.namespace.QName)
*/
public List<ConstraintDefinition> getCustomConstraintDefinitions(QName modelQName) {
mandatory("modelQName", modelQName);
Collection<ConstraintDefinition> conDefs = getDictionaryService().getConstraints(modelQName, true);
for (ConstraintDefinition conDef : conDefs) {
Constraint con = conDef.getConstraint();
if (!(con instanceof RMListOfValuesConstraint)) {
conDefs.remove(conDef);
}
}
return new ArrayList<ConstraintDefinition>(conDefs);
}
Aggregations