mardi 10 novembre 2015

Spring implementation of Flyweight pattern

I have a Flyweight pattern implementation in java programming without Spring. Please suggest how do I make this as a Spring managed Bean. It has static method calls and Inner class. The purpose is to initially load all the OptionSets from CRM to share the same OptionSets in the application. This prevents expensive calls to CRM. Users are restricted to create any new OptionSets hence Inner class. First need to implement as a Spring bean and then get it to be ApplicationContextAware to reference other beans. I am primarily using Spring XML configuration for bean definition.

public class OptionSetFactory{

private static Map <String, Object>optionSets = new HashMap();

//Inner class to restrict users creating OptionSet
private class OptionSet implements IOptionSet{
    private String entityName;
    private String attributeName;
    private Hashtable<Integer, String> options;
    private IOrganizationService service;
    private static final String GUID_EMPTY = "00000000-0000-0000-0000-000000000000";
    private ApplicationContext context;

    OptionSet(String entityName, String attributeName){
        this.entityName = entityName;
        this.attributeName = attributeName;
        //this.options = options;   

        OrganizationRequest request = new OrganizationRequest();            
        request.setRequestName("RetrieveAttribute");            
        Guid guid = new Guid();
        guid.setValue(GUID_EMPTY);                  
        ParameterCollection paramCol = new ParameterCollection();
        KeyValuePairOfstringanyType kv0 = new KeyValuePairOfstringanyType();
        kv0.setKey("MetadataId");
        kv0.setValue(guid);
        paramCol.getKeyValuePairOfstringanyTypes().add(kv0);

        KeyValuePairOfstringanyType kv1 = new KeyValuePairOfstringanyType();
        kv1.setKey("EntityLogicalName");
        kv1.setValue(entityName);
        paramCol.getKeyValuePairOfstringanyTypes().add(kv1);
        KeyValuePairOfstringanyType kv2 = new KeyValuePairOfstringanyType();
        kv2.setKey("LogicalName");
        kv2.setValue(attributeName);
        paramCol.getKeyValuePairOfstringanyTypes().add(kv2);
        KeyValuePairOfstringanyType kv3 = new KeyValuePairOfstringanyType();
        kv3.setKey("RetrieveAsIfPublished");
        kv3.setValue(true);
        paramCol.getKeyValuePairOfstringanyTypes().add(kv3);
        request.setParameters(paramCol);
        try {
            OrganizationResponse response=service.execute(request);
            PicklistAttributeMetadata pickListAttrMetadata = (PicklistAttributeMetadata)response.getResults().getKeyValuePairOfstringanyTypes().get(0).getValue();
            OptionSetMetadata optionSetMetadata = pickListAttrMetadata.getOptionSet();
            for(OptionMetadata optionMetaData : optionSetMetadata.getOptions().getOptionMetadatas()){
                //
                System.out.println("1");
                System.out.println("2");

            }

        } catch (IOrganizationServiceExecuteOrganizationServiceFaultFaultFaultMessage e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    @Override
    public String getEntityName() {

        return entityName;
    }

    @Override
    public String getAttributeName() {

        return attributeName;
    }

    @Override
    public Hashtable<Integer, String> getOptions() {

        return options;
    }



}
//static block to load predefined OptionSets in HashMap
static{
    OptionSetFactory factory = new OptionSetFactory();
    optionSets.put("dsl_type", factory.new OptionSet("dsl_type", "dsl_operationalstructure"));

}
//Static method calls for user to fetch OptionSet based on inputs
public static IOptionSet getOptionSet(String entityName, String attributeName){
    return (IOptionSet) optionSets.get(entityName+"."+attributeName);
}

public static IOptionSet getOptionSet(String attributeName){
    return (IOptionSet) optionSets.get(attributeName.toLowerCase());        
}

}

Aucun commentaire:

Enregistrer un commentaire