|
| 1 | +package it.eng.spagobi.commons.initializers.metadata; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.apache.logging.log4j.LogManager; |
| 8 | +import org.apache.logging.log4j.Logger; |
| 9 | +import org.hibernate.Session; |
| 10 | + |
| 11 | +import it.eng.spago.base.SourceBean; |
| 12 | +import it.eng.spagobi.analiticalmodel.functionalitytree.bo.LowFunctionality; |
| 13 | +import it.eng.spagobi.analiticalmodel.functionalitytree.dao.ILowFunctionalityDAO; |
| 14 | +import it.eng.spagobi.commons.bo.Role; |
| 15 | +import it.eng.spagobi.commons.dao.DAOFactory; |
| 16 | +import it.eng.spagobi.commons.dao.ITenantsDAO; |
| 17 | +import it.eng.spagobi.commons.metadata.SbiTenant; |
| 18 | +import it.eng.spagobi.utilities.assertion.Assert; |
| 19 | +import it.eng.spagobi.utilities.exceptions.SpagoBIRuntimeException; |
| 20 | + |
| 21 | +public class TreeInitializer extends SpagoBIInitializer { |
| 22 | + |
| 23 | + private static final Logger logger = LogManager.getLogger(TreeInitializer.class); |
| 24 | + |
| 25 | + private final List<SbiTenant> tenants = new ArrayList<>(); |
| 26 | + |
| 27 | + public TreeInitializer() { |
| 28 | + targetComponentName = "Tree"; |
| 29 | + configurationFileName = "it/eng/spagobi/commons/initializers/metadata/config/tree.xml"; |
| 30 | + } |
| 31 | + |
| 32 | + public List<SbiTenant> getTenants() { |
| 33 | + return tenants; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void init(SourceBean config, Session hibernateSession) { |
| 38 | + logger.debug("IN"); |
| 39 | + try { |
| 40 | + if (tenants.isEmpty()) { |
| 41 | + ITenantsDAO tenantsDAO = DAOFactory.getTenantsDAO(); |
| 42 | + tenants.addAll(tenantsDAO.loadAllTenants()); |
| 43 | + } |
| 44 | + |
| 45 | + for (SbiTenant tenant : tenants) { |
| 46 | + initialize(tenant); |
| 47 | + } |
| 48 | + } catch (Exception e) { |
| 49 | + logger.error("Error while initializing tree structure", e); |
| 50 | + throw new SpagoBIRuntimeException("Error while initializing tree structure", e); |
| 51 | + } |
| 52 | + logger.debug("OUT"); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + private void initialize(SbiTenant tenant) { |
| 57 | + Assert.assertNotNull(tenant, "Tenant in input cannot be null"); |
| 58 | + try { |
| 59 | + ILowFunctionalityDAO functionalityDAO = DAOFactory.getLowFunctionalityDAO(); |
| 60 | + functionalityDAO.setTenant(tenant.getName()); |
| 61 | + List functions = functionalityDAO.loadAllLowFunctionalities(false); |
| 62 | + if (functions != null && functions.size() > 0) { |
| 63 | + logger.debug("Tree already initialized"); |
| 64 | + } else { |
| 65 | + SourceBean configuration = getConfiguration(); |
| 66 | + List nodes = configuration.getAttributeAsList("TREE_INITIAL_STRUCTURE.NODE"); |
| 67 | + Iterator it = nodes.iterator(); |
| 68 | + while (it.hasNext()) { |
| 69 | + SourceBean node = (SourceBean) it.next(); |
| 70 | + String code = (String) node.getAttribute("code"); |
| 71 | + String name = (String) node.getAttribute("name"); |
| 72 | + String description = (String) node.getAttribute("description"); |
| 73 | + String codeType = (String) node.getAttribute("codeType"); |
| 74 | + String parentPath = (String) node.getAttribute("parentPath"); |
| 75 | + LowFunctionality functionality = new LowFunctionality(); |
| 76 | + functionality.setCode(code); |
| 77 | + functionality.setName(name); |
| 78 | + functionality.setDescription(description); |
| 79 | + functionality.setCodType(codeType); |
| 80 | + functionality.setPath(parentPath + "/" + code); |
| 81 | + if (parentPath != null && !parentPath.trim().equals("")) { |
| 82 | + // if it is not the root load the id of the parent path |
| 83 | + LowFunctionality parentFunctionality = functionalityDAO.loadLowFunctionalityByPath(parentPath, false); |
| 84 | + functionality.setParentId(parentFunctionality.getId()); |
| 85 | + } else { |
| 86 | + // if it is the root the parent path id is set to null |
| 87 | + functionality.setParentId(null); |
| 88 | + } |
| 89 | + // sets no permissions |
| 90 | + functionality.setDevRoles(new Role[0]); |
| 91 | + functionality.setExecRoles(new Role[0]); |
| 92 | + functionality.setTestRoles(new Role[0]); |
| 93 | + functionality.setCreateRoles(new Role[0]); |
| 94 | + functionalityDAO.insertLowFunctionality(functionality, null); |
| 95 | + } |
| 96 | + } |
| 97 | + } catch (Exception e) { |
| 98 | + logger.error("Error while initializing tree structure in tenant " + tenant.getName(), e); |
| 99 | + throw new SpagoBIRuntimeException("Error while initializing tree structure in tenant " + tenant.getName(), e); |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments