Source Code

From Projectivity Documentation

Jump to: navigation, search
Go back to Framework Development Guide main section

Here is the full source code of the Customer Management framework. You can also download the full Netbeans project for this framework on the Projectivity sourceforge project.

/*
 * Copyright 2008 SoftInstigate Srl
 *
 * This file is part of Projectivity.
 *
 * Projectivity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Projectivity 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Projectivity. If not, see <http://www.gnu.org/licenses/>.
 */

package customermanagement;

import com.si.projectivity.auth.Authenticator;
import com.si.projectivity.core.Constants;
import com.si.projectivity.core.business.asset.AssetDTO;
import com.si.projectivity.core.business.attachment.AttachmentGroupDTO;
import com.si.projectivity.core.business.attachment.DateMetaDataDTO;
import com.si.projectivity.core.business.attachment.MetaDataDTO;
import com.si.projectivity.core.business.attachment.NumberMetaDataDTO;
import com.si.projectivity.core.business.attachment.SerialCodeMetaDataDTO;
import com.si.projectivity.core.business.attachment.SingleListMetaDataDTO;
import com.si.projectivity.core.business.attachment.StringMetaDataDTO;
import com.si.projectivity.core.business.attachment.UrlMetaDataDTO;
import com.si.projectivity.core.business.attachment.WideStringMetaDataDTO;
import com.si.projectivity.core.business.resource.HumanResourceDTO;
import com.si.projectivity.core.business.role.WorkRoleDTO;
import com.si.projectivity.core.business.scope.definition.MentorStructuralScopeDefinitionDVO;
import com.si.projectivity.core.business.scope.definition.MetaDataReference;
import com.si.projectivity.core.business.scope.definition.PredefinedWatch;
import com.si.projectivity.core.business.scope.definition.ScopeDefinitionName;
import com.si.projectivity.core.business.scope.definition.ScopeDefinitionServiceRemote;
import com.si.projectivity.core.business.scope.definition.ScopeDefinitionServiceRemoteHome;
import com.si.projectivity.core.business.scope.definition.ScopeDefinitionWorkRole;
import com.si.projectivity.core.business.scope.definition.StructuralTemplateDTO;
import com.si.projectivity.core.business.watch.WatchDTO;
import com.si.projectivity.core.datasource.IFileSystemDataSource;
import com.si.projectivity.core.datasource.VirtualPath;
import com.si.projectivity.core.util.Pair;
import com.si.projectivity.core.util.ServiceLocator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Example customer management framework
 * @author Emmanuel Jay
 */
public class Main
{
    ScopeDefinitionServiceRemoteHome scopeDefHome = null;
    ScopeDefinitionServiceRemote     scopeDefSrv  = null;
    
    private String CUSTOMER_MANAGEMENT_FRAMEWORK_NAME    = "Customer Management Framework";
    private int    CUSTOMER_MANAGEMENT_FRAMEWORK_VERSION = 1;

    private String companyName  = "Company";
    private String regionName   = "Region";
    private String customerName = "Customer";
    
    private ScopeDefinitionName defNameCompany  = new ScopeDefinitionName(CUSTOMER_MANAGEMENT_FRAMEWORK_NAME, companyName,  CUSTOMER_MANAGEMENT_FRAMEWORK_VERSION);
    private ScopeDefinitionName defNameRegion   = new ScopeDefinitionName(CUSTOMER_MANAGEMENT_FRAMEWORK_NAME, regionName,   CUSTOMER_MANAGEMENT_FRAMEWORK_VERSION);
    private ScopeDefinitionName defNameCustomer = new ScopeDefinitionName(CUSTOMER_MANAGEMENT_FRAMEWORK_NAME, customerName, CUSTOMER_MANAGEMENT_FRAMEWORK_VERSION);
    
    // Company Roles
    private WorkRoleDTO _roleCompanyCEO         = null;     // CEO
    private WorkRoleDTO _roleCompanySALESDIR    = null;     // Sales Director
    private WorkRoleDTO _roleCompanySTAFF       = null;     // Staff

    // Region Roles
    private WorkRoleDTO _roleRegionCMNGR        = null;     // Country Manager
    private WorkRoleDTO _roleRegionAMNGRGR      = null;     // Account Managers Group
    private WorkRoleDTO _roleRegionSALESM       = null;     // Salesman

    // Customer Roles
    private WorkRoleDTO _roleCustomerAMNGR     = null;      // Account Manager
    
    /**
     * Main method to login Projectivity and create the scope definition service
     * @param password
     * @throws java.lang.Exception
     */
    public Main(String password) throws Exception
    {
        Authenticator.authenticate("admin", password);
        
        try
        {
            scopeDefHome = (ScopeDefinitionServiceRemoteHome) ServiceLocator.getInstance().getRemoteHome("ejb/Projectivity/ScopeDefinitionService");
            scopeDefSrv  = scopeDefHome.create();
        }
        catch(Throwable e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }
    
    /**
     * Command line method to install this framework
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception
    {
        if(args.length == 0)
        {
            System.out.println("Error: Admin password needed");
            System.exit(1);
        }
        
        Main me = new Main(args[0]);
      
        // Create the framework
        me.createStructural(true);
        
        System.exit(0);
    }
    
    /**
     * Create the framework
     * @param create
     * @throws java.lang.Exception
     */
    public void createStructural(boolean create) throws Exception
    {
        Date date = new Date();
        
        MentorStructuralScopeDefinitionDVO scopeDefCompany  = new MentorStructuralScopeDefinitionDVO();        
        MentorStructuralScopeDefinitionDVO scopeDefRegion   = new MentorStructuralScopeDefinitionDVO();
        MentorStructuralScopeDefinitionDVO scopeDefCustomer = new MentorStructuralScopeDefinitionDVO();

        // This is used by Projectivity to display a specific icon in the workspace tree hierarchy
        // Company will be under the ROOT
        scopeDefCompany.setKeyPrefix(Constants.KEY_PREFIX_SCOPE_DEFAULT_STRUCTURAL_COMPANY);
        // Region will be under a Company
        scopeDefRegion.setKeyPrefix(Constants.KEY_PREFIX_SCOPE_DEFAULT_STRUCTURAL_DEPARTMENT);
        // Customer will be under a region
        scopeDefCustomer.setKeyPrefix(Constants.KEY_PREFIX_SCOPE_DEFAULT_STRUCTURAL_OFFICE);

        // ---------------------------------------------------------------------
        // ROLES
        // ---------------------------------------------------------------------
        
        // Company Roles (CEO, Sales Director, Staff)
        _roleCompanyCEO      = new WorkRoleDTO(null, "CEO", "CEO", 1, 1);
        _roleCompanySALESDIR = new WorkRoleDTO(null, "Sales Director", "Sales Director", 1, 1);
        _roleCompanySTAFF    = new WorkRoleDTO(null, "Staff", "Staff", 0, WorkRoleDTO.STAR_CARDINALITY);

        // Region Roles (Country Manager, Account Managers Group, Salesman) 
        _roleRegionCMNGR     = new WorkRoleDTO(null, "Country Manager", "Country Manager", 1, 1);      
        _roleRegionAMNGRGR   = new WorkRoleDTO(null, "Account Managers Group", "Account Managers Group", 0, WorkRoleDTO.STAR_CARDINALITY);      
        _roleRegionSALESM    = new WorkRoleDTO(null, "Sales Man", "Sales Man", 0, WorkRoleDTO.STAR_CARDINALITY);

        // Customer Roles (Account Manager)
        _roleCustomerAMNGR   = new WorkRoleDTO(null, "Account Manager", "Account Manager", 1, 1);              
        
        // Set the scope definition for the Company roles
        _roleCompanyCEO.setScopeDefinition(defNameCompany);
        _roleCompanySALESDIR.setScopeDefinition(defNameCompany);
        _roleCompanySTAFF.setScopeDefinition(defNameCompany);

        // Set the Scope Definition for the Region roles
        _roleRegionCMNGR.setScopeDefinition(defNameRegion);
        _roleRegionAMNGRGR.setScopeDefinition(defNameRegion);
        _roleRegionSALESM.setScopeDefinition(defNameRegion);

        // Set the Scope Definition for the Customer role
        _roleCustomerAMNGR.setScopeDefinition(defNameCustomer);

        // Now create all the scope definition work role objects
        ScopeDefinitionWorkRole roleCompanyCEO      = new ScopeDefinitionWorkRole(_roleCompanyCEO,      HumanResourceDTO.SecurityRole.OWNER,  false, 1, 1);
        ScopeDefinitionWorkRole roleCompanySALESDIR = new ScopeDefinitionWorkRole(_roleCompanySALESDIR, HumanResourceDTO.SecurityRole.MEMBER, false, 1, 1);
        ScopeDefinitionWorkRole roleCompanySTAFF    = new ScopeDefinitionWorkRole(_roleCompanySTAFF,    HumanResourceDTO.SecurityRole.MEMBER, false, 0, WorkRoleDTO.STAR_CARDINALITY);

        ScopeDefinitionWorkRole roleRegionCMNGR     = new ScopeDefinitionWorkRole(_roleRegionCMNGR,     HumanResourceDTO.SecurityRole.OWNER, false,  1, 1);
        ScopeDefinitionWorkRole roleRegionAMNGRGR   = new ScopeDefinitionWorkRole(_roleRegionAMNGRGR,   HumanResourceDTO.SecurityRole.OWNER , false, 0, WorkRoleDTO.STAR_CARDINALITY);
        ScopeDefinitionWorkRole roleRegionSALESM    = new ScopeDefinitionWorkRole(_roleRegionSALESM,    HumanResourceDTO.SecurityRole.OWNER, false,  0, WorkRoleDTO.STAR_CARDINALITY);

        ScopeDefinitionWorkRole roleCustomerAMNGR   = new ScopeDefinitionWorkRole(_roleCustomerAMNGR,   HumanResourceDTO.SecurityRole.OWNER, false,  1, 1);

        // Create a Collection of roles for the Company type   
        Collection<ScopeDefinitionWorkRole> rolesCompany = new ArrayList<ScopeDefinitionWorkRole>();

        rolesCompany.add(roleCompanyCEO);
        rolesCompany.add(roleCompanySALESDIR);
        rolesCompany.add(roleCompanySTAFF);

        // Now associate these role to the Company scope definition  
        scopeDefCompany.setScopeDefinitionWorkRoles(rolesCompany);

        // Create a Collection of roles for the Region type
        Collection<ScopeDefinitionWorkRole> rolesRegion = new ArrayList<ScopeDefinitionWorkRole>();

        rolesRegion.add(roleRegionCMNGR);
        rolesRegion.add(roleRegionAMNGRGR);
        rolesRegion.add(roleRegionSALESM);

        // Now associate these roles to the Region scope definition       
        scopeDefRegion.setScopeDefinitionWorkRoles(rolesRegion);

        // Create a Collection of roles for the Customer type
        Collection<ScopeDefinitionWorkRole> rolesCustomer = new ArrayList<ScopeDefinitionWorkRole>();

        rolesCustomer.add(roleCustomerAMNGR);

        // Now associate these role to the Customer scope definition  
        scopeDefCustomer.setScopeDefinitionWorkRoles(rolesCustomer);
        
        // ---------------------------------------------------------------------
        // METADATA
        // ---------------------------------------------------------------------
        
        // Metadata for the Company Workspace
        MetaDataDTO mdCompanyWebSite = new UrlMetaDataDTO("custmngmt.company.website", date, "http://www.mycompany.com/");
        MetaDataDTO mdCompanySlogan  = new StringMetaDataDTO("custmngmt.company.slogan", date, "");

        Collection<MetaDataDTO> mdCompany = new ArrayList<MetaDataDTO>();
        mdCompany.add(mdCompanyWebSite);
        mdCompany.add(mdCompanySlogan);
        
        // Metadata for the Region Workspace
        MetaDataDTO mdRegionDefinition   = new WideStringMetaDataDTO("custmngmt.region.definition", date, "");

        Collection<MetaDataDTO> mdRegion = new ArrayList<MetaDataDTO>();
        mdRegion.add(mdRegionDefinition);
        
        // Metadata for the Customer Workspace
        List<String> industries = new ArrayList<String>();
        industries.add("Telecom");
        industries.add("Automotiv");

        List<String> statuss = new ArrayList<String>();
        statuss.add("Lead");
        statuss.add("Customer");
        statuss.add("Closed");

        MetaDataDTO mdCustomerCode           = new SerialCodeMetaDataDTO("custmngmt.customer.code", "YYYY", "000", true); 
        MetaDataDTO mdCustomerCreationDate   = new DateMetaDataDTO("custmngmt.customer.creationdate", date, date);
        MetaDataDTO mdCustomerFullName       = new StringMetaDataDTO("custmngmt.customer.fullname", date, "");
        MetaDataDTO mdCustomerAddress        = new WideStringMetaDataDTO("custmngmt.customer.address", date, "");
        MetaDataDTO mdCustomerBillingAddress = new WideStringMetaDataDTO("custmngmt.customer.address.billing", date, "");
        MetaDataDTO mdCustomerVATNumber      = new NumberMetaDataDTO("custmngmt.customer.vatnumber", date, new Integer(0));
        MetaDataDTO mdCustomerIndustry       = new SingleListMetaDataDTO("custmngmt.customer.industry", date, industries);
        MetaDataDTO mdCustomerStatus         = new SingleListMetaDataDTO("custmngmt.customer.status", date, statuss);

        Collection<MetaDataDTO> mdCustomer = new ArrayList<MetaDataDTO>();
        mdCustomer.add(mdCustomerCode);
        mdCustomer.add(mdCustomerCreationDate);
        mdCustomer.add(mdCustomerFullName);
        mdCustomer.add(mdCustomerAddress);
        mdCustomer.add(mdCustomerBillingAddress);
        mdCustomer.add(mdCustomerVATNumber);
        mdCustomer.add(mdCustomerIndustry);
        mdCustomer.add(mdCustomerStatus);

        // Lets now associate the metadata fields to their respective workspace types:
        scopeDefCompany.setMetaDataSet(mdCompany);
        scopeDefRegion.setMetaDataSet(mdRegion);
        scopeDefCustomer.setMetaDataSet(mdCustomer);

        
        // ---------------------------------------------------------------------
        // TOOLS
        // ---------------------------------------------------------------------
         
        // This framework does not define any tools, however you will find below
        // a sample piece of code showing you how to define tools for a workspace
        /*
        Collection<AttachmentGroupDTO> tools  = new ArrayList<AttachmentGroupDTO>();
        AttachmentGroupDTO notes    = new AttachmentGroupDTO("Notes", "Notes",   AttachmentDTO.AttachmentType.NOTE);
        AttachmentGroupDTO meetings = new AttachmentGroupDTO("Meetings", "Meetings", AttachmentDTO.AttachmentType.MEETING);
        AttachmentGroupDTO tasks    = new AttachmentGroupDTO("Tasks", "Tasks",       AttachmentDTO.AttachmentType.TASK);
        tools.add(notes);
        tools.add(meetings);
        tools.add(tasks);
        */
        // Just create an empty collection if you do not want any tools
        Collection<AttachmentGroupDTO> noTools  = new ArrayList<AttachmentGroupDTO>();
        
        // Set the tools (set to you own collection of tools)
        scopeDefCompany.setTools(noTools);
        scopeDefRegion.setTools(noTools);
        scopeDefCustomer.setTools(noTools);
                
        // ---------------------------------------------------------------------
        // DEFAULT NAMES
        // ---------------------------------------------------------------------
        
        // Here we set the default names for the workpsaces that will be auto 
        // created. Our framework definition says that when a company is created:
        //   - 2 regions must be created
        //   - the regions must be called Italy and France (by default)
        Collection<String> defaultRegionNames = new ArrayList<String>();
        
        defaultRegionNames.add("Italy");
        defaultRegionNames.add("France");
        
        scopeDefRegion.setDefaultsScopeNames(defaultRegionNames);
        
        // ---------------------------------------------------------------------
        // ASSETS
        // ---------------------------------------------------------------------
        
        // Lets create the Customer Profile directory
        AssetDTO customerProfileAsset = new AssetDTO();

        customerProfileAsset.setDescription("Profile of this customer");
        VirtualPath pathCustomerProfile = new VirtualPath("/Customer Profile");

        customerProfileAsset.setPath(pathCustomerProfile);
        customerProfileAsset.setStatus(AssetDTO.AssetStatus.PUBLIC);
        customerProfileAsset.setType(IFileSystemDataSource.FileType.DIR);

        // Lets now create the Orders directory
        AssetDTO ordersAsset = new AssetDTO();

        ordersAsset.setDescription("Orders");
        VirtualPath pathOrders = new VirtualPath("/Orders");

        ordersAsset.setPath(pathOrders);
        ordersAsset.setStatus(AssetDTO.AssetStatus.PUBLIC);
        ordersAsset.setType(IFileSystemDataSource.FileType.DIR);

        // Lets now create the Invoices directory
        AssetDTO invoicesAsset = new AssetDTO();

        ordersAsset.setDescription("Invoices");
        VirtualPath pathInvoices = new VirtualPath("/Invoices");

        invoicesAsset.setPath(pathInvoices);
        invoicesAsset.setStatus(AssetDTO.AssetStatus.PUBLIC);
        invoicesAsset.setType(IFileSystemDataSource.FileType.DIR);

        // Lets now add this directory structure to the assets of the Customer workspace         
        Collection<AssetDTO> customerAssets = new ArrayList<AssetDTO>();

        customerAssets.add(customerProfileAsset);
        customerAssets.add(ordersAsset);
        customerAssets.add(invoicesAsset);

        scopeDefCustomer.setScopeDefinitionAsset(customerAssets);

        // ---------------------------------------------------------------------
        // WATCHES
        // ---------------------------------------------------------------------
        
        // We now define 2 watches: 1 for the sales director and another for the ceo
        PredefinedWatch newCustomer_SalesDir = new PredefinedWatch(_roleCompanySALESDIR, defNameCompany);
        newCustomer_SalesDir.setCustomTemplateKey("custmanagement_new_customer_salesdir");
        newCustomer_SalesDir.setOneShotCondition();

        PredefinedWatch newCustomer_CEO = new PredefinedWatch(_roleCompanyCEO, defNameCompany);
        newCustomer_CEO.setCustomTemplateKey("custmanagement_new_customer_ceo");
        newCustomer_CEO.setOneShotCondition();

        // Create a collection of watches for the customer type
        Collection<PredefinedWatch> customerWatches = new ArrayList<PredefinedWatch>();        
        customerWatches.add(newCustomer_SalesDir);
        customerWatches.add(newCustomer_CEO);

        // Now create the watch to send an email to the account manager when the customer status is modified
        MetaDataReference custStatusRef = new MetaDataReference(defNameCustomer, "State");

        PredefinedWatch custStatusChangeWatch_AccountMngr = new PredefinedWatch(_roleCustomerAMNGR,  defNameCustomer, custStatusRef, WatchDTO.ConditionOperator.UPDATED, "", false);    
        custStatusChangeWatch_AccountMngr.setOneShotCondition();
        custStatusChangeWatch_AccountMngr.setCustomTemplateKey("custmanagement_customer_state_accountmngr");

        // Now add to the list of watches
        customerWatches.add(custStatusChangeWatch_AccountMngr);

        // Now create a watch that is fired when the document structure of a Customer is modified
        VirtualPath rootPath = new VirtualPath("/");

        PredefinedWatch modifDocumentCustWatch_AccountManager = new PredefinedWatch(rootPath, _roleCustomerAMNGR, defNameCustomer);
        modifDocumentCustWatch_AccountManager.setOneShotCondition();
        modifDocumentCustWatch_AccountManager.setCustomTemplateKey("custmanagement_customer_doc_accountmngr");

        // Now add to the list of watches
        customerWatches.add(modifDocumentCustWatch_AccountManager);
        
        // ---------------------------------------------------------------------
        // ORGANISE AND CREATE THE FRAMEWORK
        // ---------------------------------------------------------------------
        
        // Define the required cardinalities
        Pair<Integer, Integer> zeroStar = new Pair<Integer, Integer>(0, -1);
        Pair<Integer, Integer> twoTwo   = new Pair<Integer, Integer>(2, 2);

        Collection<Pair<ScopeDefinitionName, Pair<Integer, Integer>>> parentsCompany = new ArrayList<Pair<ScopeDefinitionName, Pair<Integer, Integer>>>();
        Collection<Pair<ScopeDefinitionName, Pair<Integer, Integer>>> parentsRegion = new ArrayList<Pair<ScopeDefinitionName, Pair<Integer, Integer>>>();
        Collection<Pair<ScopeDefinitionName, Pair<Integer, Integer>>> parentsCustomer = new ArrayList<Pair<ScopeDefinitionName, Pair<Integer, Integer>>>();

        // Any number of Company can be created under the ROOT
        parentsCompany.add(new Pair<ScopeDefinitionName, Pair<Integer, Integer>>(ScopeDefinitionName.getRootName(), zeroStar));        
        parentsRegion.add(new Pair<ScopeDefinitionName, Pair<Integer, Integer>>(defNameCompany, twoTwo));
        parentsCustomer.add(new Pair<ScopeDefinitionName, Pair<Integer, Integer>>(defNameRegion, zeroStar));

        // Now create the framework
        StructuralTemplateDTO customerManagementFramework = new StructuralTemplateDTO(CUSTOMER_MANAGEMENT_FRAMEWORK_NAME, CUSTOMER_MANAGEMENT_FRAMEWORK_VERSION);

        customerManagementFramework.put2(defNameCompany, scopeDefCompany,  parentsCompany);
        customerManagementFramework.put2(defNameRegion,  scopeDefRegion,   parentsRegion); 
        customerManagementFramework.put2(defNameCustomer,  scopeDefCustomer,   parentsCustomer); 

        if (create)
        {
            // Now create the framework
            scopeDefSrv.createDefinition(customerManagementFramework);
        }
        
        System.out.println("The Customer Management Framework was successfully created");
    }
}

Personal tools