Search in sources :

Example 1 with EntityAttributeConfigAction

use of com.agiletec.apsadmin.system.entity.type.EntityAttributeConfigAction in project entando-core by entando.

the class TestEntityManagersAction method testSaveNewAttribute.

public void testSaveNewAttribute() throws Throwable {
    // Search an entity manager
    String[] defNames = this.getApplicationContext().getBeanNamesForType(ApsEntityManager.class);
    if (null == defNames || defNames.length == 0)
        return;
    String entityManagerName = defNames[0];
    IEntityManager entityManager = (IEntityManager) this.getApplicationContext().getBean(entityManagerName);
    // get the entites managed by the ApsEntityManager
    Map<String, IApsEntity> entities = entityManager.getEntityPrototypes();
    if (null == entities || entities.size() == 0)
        return;
    List<String> enitiesTypeCodes = new ArrayList<String>();
    enitiesTypeCodes.addAll(entities.keySet());
    // get the first entity type code available
    String entityTypeCode = enitiesTypeCodes.get(0);
    try {
        // wrong name
        String attributeName = "wrong name";
        String attributeTypeCode = "Monotext";
        int strutsAction = ApsAdminSystemConstants.ADD;
        String result = this.executeSaveAttribute("admin", attributeName, attributeTypeCode, entityTypeCode, strutsAction, null, null, entityManagerName);
        assertEquals(Action.INPUT, result);
        EntityAttributeConfigAction action = (EntityAttributeConfigAction) this.getAction();
        Map<String, List<String>> fieldErrors = action.getFieldErrors();
        assertEquals(1, fieldErrors.size());
        assertTrue(fieldErrors.containsKey("attributeName"));
        // wrong length
        attributeName = "right_name";
        result = this.executeSaveAttribute("admin", attributeName, attributeTypeCode, entityTypeCode, strutsAction, 3, 5, entityManagerName);
        assertEquals(Action.INPUT, result);
        fieldErrors = action.getFieldErrors();
        assertEquals(1, fieldErrors.size());
        assertTrue(fieldErrors.containsKey("minLength"));
        // insert ok
        attributeName = "right_name";
        result = this.executeSaveAttribute("admin", attributeName, attributeTypeCode, entityTypeCode, strutsAction, 5, 3, entityManagerName);
        assertEquals(Action.SUCCESS, result);
        IApsEntity entity = entityManager.getEntityPrototype(entityTypeCode);
        assertTrue(entity.getAttributeMap().containsKey("right_name"));
        // duplicate attribute name
        attributeName = "right_name";
        result = this.executeSaveAttribute("admin", attributeName, attributeTypeCode, entityTypeCode, strutsAction, null, null, entityManagerName);
        assertEquals(Action.INPUT, result);
        fieldErrors = action.getFieldErrors();
        assertEquals(1, fieldErrors.size());
        assertTrue(fieldErrors.containsKey("attributeName"));
    } catch (Throwable t) {
    // nothing to do
    } finally {
        IApsEntity entity = entityManager.getEntityPrototype(entityTypeCode);
        entity.getAttributeMap().remove("right_name");
        ((IEntityTypesConfigurer) entityManager).updateEntityPrototype(entity);
    }
}
Also used : ArrayList(java.util.ArrayList) IEntityManager(com.agiletec.aps.system.common.entity.IEntityManager) IApsEntity(com.agiletec.aps.system.common.entity.model.IApsEntity) ArrayList(java.util.ArrayList) List(java.util.List) IEntityTypesConfigurer(com.agiletec.aps.system.common.entity.IEntityTypesConfigurer) EntityAttributeConfigAction(com.agiletec.apsadmin.system.entity.type.EntityAttributeConfigAction)

Example 2 with EntityAttributeConfigAction

use of com.agiletec.apsadmin.system.entity.type.EntityAttributeConfigAction in project entando-core by entando.

the class TestEntityManagersAction method testEditAttribute.

public void testEditAttribute() throws Throwable {
    // Search an entity manager
    String[] defNames = this.getApplicationContext().getBeanNamesForType(ApsEntityManager.class);
    if (null == defNames || defNames.length == 0)
        return;
    String entityManagerName = defNames[0];
    IEntityManager entityManager = (IEntityManager) this.getApplicationContext().getBean(entityManagerName);
    // get the entites managed by the ApsEntityManager
    Map<String, IApsEntity> entities = entityManager.getEntityPrototypes();
    if (null == entities || entities.size() == 0)
        return;
    List<String> enitiesTypeCodes = new ArrayList<String>();
    enitiesTypeCodes.addAll(entities.keySet());
    // get the first entity type code available
    String entityTypeCode = enitiesTypeCodes.get(0);
    List<AttributeInterface> attributes = entities.get(entityTypeCode).getAttributeList();
    // get the first attribute
    for (int i = 0; i < attributes.size(); i++) {
        AttributeInterface currentAttribute = attributes.get(i);
        String attributeName = currentAttribute.getName();
        String result = this.executeEditAttribute("admin", attributeName, entityTypeCode, entityManagerName);
        assertEquals(Action.SUCCESS, result);
        EntityAttributeConfigAction action = (EntityAttributeConfigAction) this.getAction();
        assertEquals(currentAttribute.getType(), action.getAttributeTypeCode());
        assertEquals(currentAttribute.isRequired(), action.getRequired().booleanValue());
        assertEquals(currentAttribute.isSearchable(), action.getSearchable().booleanValue());
        assertEquals(currentAttribute.getIndexingType().equalsIgnoreCase(IndexableAttributeInterface.INDEXING_TYPE_TEXT), action.getIndexable().booleanValue());
        if (currentAttribute.isTextAttribute()) {
            ITextAttribute attr = (ITextAttribute) currentAttribute;
            if (attr.getMaxLength() == -1) {
                assertNull(action.getMaxLength());
            } else {
                assertEquals(attr.getMaxLength(), action.getMaxLength().intValue());
            }
            if (attr.getMinLength() == -1) {
                assertNull(action.getMinLength());
            } else {
                assertEquals(attr.getMinLength(), action.getMinLength().intValue());
            }
            assertEquals(attr.getRegexp(), action.getRegexp());
        }
        assertEquals(ApsAdminSystemConstants.EDIT, action.getStrutsAction());
    }
}
Also used : ITextAttribute(com.agiletec.aps.system.common.entity.model.attribute.ITextAttribute) IEntityManager(com.agiletec.aps.system.common.entity.IEntityManager) ArrayList(java.util.ArrayList) IApsEntity(com.agiletec.aps.system.common.entity.model.IApsEntity) IndexableAttributeInterface(com.agiletec.aps.system.common.searchengine.IndexableAttributeInterface) AttributeInterface(com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface) EntityAttributeConfigAction(com.agiletec.apsadmin.system.entity.type.EntityAttributeConfigAction)

Aggregations

IEntityManager (com.agiletec.aps.system.common.entity.IEntityManager)2 IApsEntity (com.agiletec.aps.system.common.entity.model.IApsEntity)2 EntityAttributeConfigAction (com.agiletec.apsadmin.system.entity.type.EntityAttributeConfigAction)2 ArrayList (java.util.ArrayList)2 IEntityTypesConfigurer (com.agiletec.aps.system.common.entity.IEntityTypesConfigurer)1 AttributeInterface (com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface)1 ITextAttribute (com.agiletec.aps.system.common.entity.model.attribute.ITextAttribute)1 IndexableAttributeInterface (com.agiletec.aps.system.common.searchengine.IndexableAttributeInterface)1 List (java.util.List)1