PL-1202 Разработать механизм работы с динамическими атрибутами

add javadocs
#PL-1202
This commit is contained in:
Eugeny Degtyarjov 2015-04-23 14:33:04 +00:00
parent 7da4832148
commit e22255a09c
5 changed files with 57 additions and 9 deletions

View File

@ -112,13 +112,13 @@ public class DynamicAttributesManager implements DynamicAttributesManagerAPI {
}
@Override
public Collection<Category> getCategoriesForMetaClass(@Nullable MetaClass metaClass) {
public Collection<Category> getCategoriesForMetaClass(MetaClass metaClass) {
MetaClass targetMetaClass = resolveTargetMetaClass(metaClass);
return new ArrayList<>(categories().get(targetMetaClass));
}
@Override
public Collection<CategoryAttribute> getAttributesForMetaClass(@Nullable MetaClass metaClass) {
public Collection<CategoryAttribute> getAttributesForMetaClass(MetaClass metaClass) {
MetaClass targetMetaClass = resolveTargetMetaClass(metaClass);
Collection<Category> categories = categories().get(targetMetaClass);
List<CategoryAttribute> categoryAttributes = new ArrayList<>();
@ -135,7 +135,7 @@ public class DynamicAttributesManager implements DynamicAttributesManagerAPI {
@Nullable
@Override
public CategoryAttribute getAttributeForMetaClass(@Nullable MetaClass metaClass, String code) {
public CategoryAttribute getAttributeForMetaClass(MetaClass metaClass, String code) {
MetaClass targetMetaClass = resolveTargetMetaClass(metaClass);
Map<String, CategoryAttribute> attributes = attributes().get(targetMetaClass);
if (attributes != null) {
@ -179,7 +179,7 @@ public class DynamicAttributesManager implements DynamicAttributesManagerAPI {
}
}
protected MetaClass resolveTargetMetaClass(@Nullable MetaClass metaClass) {
protected MetaClass resolveTargetMetaClass(MetaClass metaClass) {
if (metaClass == null) {
return null;
}

View File

@ -19,12 +19,24 @@ import java.util.Collection;
public interface DynamicAttributesManagerAPI {
String NAME = "cuba_DynamicAttributesManager";
/**
* Reload dynamic attributes cache from database
*/
void loadCache();
/**
* Get all categories linked with metaClass from cache
*/
Collection<Category> getCategoriesForMetaClass(MetaClass metaClass);
/**
* Get all categories attributes for metaClass from cache
*/
Collection<CategoryAttribute> getAttributesForMetaClass(MetaClass metaClass);
/**
* Get certain category attribute for metaClass by attribute code
*/
@Nullable
CategoryAttribute getAttributeForMetaClass(MetaClass metaClass, String code);
}

View File

@ -19,12 +19,24 @@ import java.util.Collection;
public interface DynamicAttributesService {
String NAME = "cuba_DynamicAttributesService";
/**
* Reload dynamic attributes cache from database
*/
void loadCache();
/**
* Get all categories linked with metaClass from cache
*/
Collection<Category> getCategoriesForMetaClass(MetaClass metaClass);
/**
* Get all categories attributes for metaClass from cache
*/
Collection<CategoryAttribute> getAttributesForMetaClass(MetaClass metaClass);
/**
* Get certain category attribute for metaClass by attribute's code
*/
@Nullable
CategoryAttribute getAttributeForMetaClass(MetaClass metaClass, String code);
}

View File

@ -25,37 +25,54 @@ public final class DynamicAttributesUtils {
private DynamicAttributesUtils() {
}
/**
* Get special meta property path object for dynamic attribute
*/
public static MetaPropertyPath getMetaPropertyPath(MetaClass metaClass, CategoryAttribute attribute) {
MetaProperty metaProperty = new DynamicAttributesMetaProperty(metaClass, attribute);
return new MetaPropertyPath(metaClass, metaProperty);
}
/**
* Get special meta property path object for dynamic attribute by code
*/
@Nullable
public static MetaPropertyPath getMetaPropertyPath(MetaClass metaClass, String attributeCode) {
attributeCode = decodeAttributeCode(attributeCode);
CategoryAttribute attribute =
AppBeans.get(DynamicAttributesService.class).getAttributeForMetaClass(metaClass, attributeCode);
CategoryAttribute attribute = AppBeans.get(DynamicAttributesService.NAME, DynamicAttributesService.class)
.getAttributeForMetaClass(metaClass, attributeCode);
if (attribute != null) {
MetaProperty metaProperty = new DynamicAttributesMetaProperty(metaClass, attribute);
return new MetaPropertyPath(metaClass, metaProperty);
return getMetaPropertyPath(metaClass, attribute);
} else {
return null;
}
}
/**
* Remove dynamic attribute marker (+) from attribute code (if exists)
*/
public static String decodeAttributeCode(String attributeCode) {
return attributeCode.startsWith("+") ? attributeCode.substring(1) : attributeCode;
}
/**
* Add dynamic attribute marker (+) to attribute code (if does not exist)
*/
public static String encodeAttributeCode(String attributeCode) {
return attributeCode.startsWith("+") ? attributeCode : "+" + attributeCode;
}
/**
* Check if the name has dynamic attribute marker
*/
public static boolean isDynamicAttribute(String name) {
return name.startsWith("+");
}
/**
* Resolve attribute value's Java class
*/
public static Class getAttributeClass(CategoryAttribute attribute) {
if (BooleanUtils.isTrue(attribute.getIsEntity())) {

View File

@ -38,6 +38,9 @@ public class DynamicAttributesGuiTools {
@Inject
protected MetadataTools metadataTools;
/**
* Enforce the datasource to change modified status if dynamic attribute is changed
*/
@SuppressWarnings("unchecked")
public void listenDynamicAttributesChanges(final Datasource datasource) {
if (datasource != null && datasource.getLoadDynamicAttributes()) {
@ -52,7 +55,11 @@ public class DynamicAttributesGuiTools {
}
}
public Set<CategoryAttribute> getAttributesToShowOnTheScreen(MetaClass metaClass, String screen, String component) {
/**
* Get attributes which should be added automatically to the screen and component.
* Based on visibility settings from category attribute editor.
*/
public Set<CategoryAttribute> getAttributesToShowOnTheScreen(MetaClass metaClass, String screen, @Nullable String component) {
Collection<CategoryAttribute> attributesForMetaClass =
dynamicAttributesService.getAttributesForMetaClass(metaClass);
Set<CategoryAttribute> categoryAttributes = new HashSet<>();