All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.agiletec.apsadmin.system.entity.AbstractApsEntityFinderAction Maven / Gradle / Ivy

Go to download

Entando Administration Console: an agile, modern and user-centric open source Portal platform.

There is a newer version: 6.2.38
Show newest version
/*
 * Copyright 2015-Present Entando Inc. (http://www.entando.com) All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
package com.agiletec.apsadmin.system.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.agiletec.aps.system.common.entity.IEntityManager;
import com.agiletec.aps.system.common.entity.model.EntitySearchFilter;
import com.agiletec.aps.system.common.entity.model.IApsEntity;
import com.agiletec.aps.system.common.entity.model.attribute.AttributeInterface;
import com.agiletec.aps.system.common.entity.model.attribute.AttributeRole;
import com.agiletec.apsadmin.system.BaseAction;

/**
 * @author E.Santoboni
 */
public abstract class AbstractApsEntityFinderAction extends BaseAction implements IApsEntityFinderAction {

	private static final Logger _logger = LoggerFactory.getLogger(AbstractApsEntityFinderAction.class);
	
	@Override
	public String execute() {
		try {
			this.createBaseFilters();
		} catch (Throwable t) {
			_logger.error("error in execute", t);
			return FAILURE;
		}
		return SUCCESS;
	}
	
	protected void createBaseFilters() {
		try {
			int initSize = this.getFilters().length;
			EntitySearchFilter[] roleFilters = this.getEntityActionHelper().getRoleFilters(this);
			this.addFilters(roleFilters);
			IApsEntity prototype = this.getEntityPrototype();
			if (null != prototype) {
				EntitySearchFilter filterToAdd = new EntitySearchFilter(IEntityManager.ENTITY_TYPE_CODE_FILTER_KEY, false, prototype.getTypeCode(), false);
				this.addFilter(filterToAdd);
				EntitySearchFilter[] filters = this.getEntityActionHelper().getAttributeFilters(this, prototype);
				this.addFilters(filters);
			}
			this.setAddedAttributeFilter(this.getFilters().length > initSize);
		} catch (Throwable t) {
			_logger.error("Error while creating entity filters", t);
			//ApsSystemUtils.logThrowable(t, this, "createBaseFilters");
			throw new RuntimeException("Error while creating entity filters", t);
		}
	}
	
	@Override
	public List getSearchResult() {
		List result = null;
		try {
			IEntityManager entityManager = this.getEntityManager();
			result = entityManager.searchId(this.getFilters());
		} catch (Throwable t) {
			_logger.error("Error while searching entity Ids", t);
			throw new RuntimeException("Error while searching entity Ids", t);
		}
		return result;
	}
	
	protected void addFilters(EntitySearchFilter[] filters) {
		for (int i = 0; i < filters.length; i++) {
			EntitySearchFilter filterToAdd = filters[i];
			this.addFilter(filterToAdd);
		}
	}
	
	protected void addFilter(EntitySearchFilter filterToAdd) {
		EntitySearchFilter[] filters = this.getFilters();
		int len = filters.length;
		EntitySearchFilter[] newFilters = new EntitySearchFilter[len + 1];
		for(int i=0; i < len; i++){
			newFilters[i] = filters[i];
		}
		newFilters[len] = filterToAdd;
		this.setFilters(newFilters);
	}
	
	@Override
	public String trash() {
		try {
			String checkResult = this.checkDeletingEntity();
			if (null != checkResult) return checkResult;
		} catch (Throwable t) {
			_logger.error("Error while trashing entity", t);
			throw new RuntimeException("Error while trashing entity", t);
		}
		return SUCCESS;
	}
	
	@Override
	public String delete() {
		try {
			String checkResult = this.checkDeletingEntity();
			if (null != checkResult) return checkResult;
			this.deleteEntity(this.getEntityId());
		} catch (Throwable t) {
			_logger.error("Error while deleting entity", t);
			throw new RuntimeException("Error while deleting entity", t);
		}
		return SUCCESS;
	}
	
	protected abstract void deleteEntity(String entityId) throws Throwable;
	
	protected String checkDeletingEntity() throws Throwable {
		IApsEntity entity = this.getEntity(this.getEntityId());
		if (null == entity) {
			String[] args = {this.getEntityId()};
			this.addFieldError("entityId", this.getText("error.entity.null",args));
			return INPUT;
		}
		return null;
	}
	
	public IApsEntity getEntityPrototype() {
		IEntityManager entityManager = this.getEntityManager();
		return entityManager.getEntityPrototype(this.getEntityTypeCode());
	}
	
	public List getEntityPrototypes() {
		List entityPrototypes = null;
		try {
			Map modelMap = this.getEntityManager().getEntityPrototypes();
			entityPrototypes = new ArrayList(modelMap.values());
			BeanComparator comparator = new BeanComparator("typeDescr");
			Collections.sort(entityPrototypes, comparator);
		} catch (Throwable t) {
			_logger.error("Error while extracting entity prototypes", t);
			throw new RuntimeException("Error while extracting entity prototypes", t);
		}
		return entityPrototypes;
	}
	
	protected IApsEntity getEntity(String entityId) {
		IApsEntity entity = null;
		try {
			IEntityManager entityManager = this.getEntityManager();
			entity = entityManager.getEntity(entityId);
		} catch (Throwable t) {
			_logger.error("Error while extracting entity", t);
			throw new RuntimeException("Error while extracting entity", t);
		}
		return entity;
	}
	
	public String getSearchFormFieldValue(String inputFieldName) {
		String val = this.getRequest().getParameter(inputFieldName);
		if (StringUtils.isBlank(val)) {
			val = (String) this.getRequest().getAttribute(inputFieldName);
		}
		return val;
	}
	
	@Deprecated
	public List getSearcheableAttributes() {
		return this.getSearchableAttributes();
	}
	
	public List getSearchableAttributes() {
		List searchableAttributes = new ArrayList();
		IApsEntity prototype = this.getEntityPrototype();
		if (null == prototype) {
			return searchableAttributes;
		}
		List contentAttributes = prototype.getAttributeList();
		for (int i=0; i getAttributeRoles() {
		return this.getEntityManager().getAttributeRoles();
	}
	
	protected abstract IEntityManager getEntityManager();
	
	public String getEntityId() {
		return _entityId;
	}
	public void setEntityId(String entityId) {
		this._entityId = entityId;
	}
	
	public String getEntityTypeCode() {
		return _entityTypeCode;
	}
	public void setEntityTypeCode(String entityTypeCode) {
		this._entityTypeCode = entityTypeCode;
	}
	
	protected boolean isAddedAttributeFilter() {
		return _addedAttributeFilter;
	}
	protected void setAddedAttributeFilter(boolean addedAttributeFilter) {
		this._addedAttributeFilter = addedAttributeFilter;
	}
	
	protected IEntityActionHelper getEntityActionHelper() {
		return _entityActionHelper;
	}
	public void setEntityActionHelper(IEntityActionHelper entityActionHelper) {
		this._entityActionHelper = entityActionHelper;
	}
	
	protected EntitySearchFilter[] getFilters() {
		return _filters;
	}
	protected void setFilters(EntitySearchFilter[] filters) {
		this._filters = filters;
	}
	
	private String _entityId;
	private String _entityTypeCode;
	
	private boolean _addedAttributeFilter;
	
	private IEntityActionHelper _entityActionHelper;
	
	private EntitySearchFilter[] _filters = new EntitySearchFilter[0];
	
} 




© 2015 - 2025 Weber Informatics LLC | Privacy Policy