Java enum methods with current enum name

Posted: May 6, 2014 in web
public enum TEMPTemplate {
    MAT_DOT_GREEN_USAGEBASED("MATRunnerBundledUsageBased.pdf"), MAT_DOT_UNGREEN_USAGEBASED("MATRunnerUnbundledUsageBased.pdf"), MAT_VARIABLE_UNGREEN_FLAT("MATVariableUnbundledFlat.pdf"), VE_DOT_GREEN_USAGEBASED("VERunnerBundledUsageBased.pdf"), VE_VARIABLE_GREEN_FLAT("VEVariableBundledFlat.pdf");
    private static final String TEMPLATE_URL = "http://serverv/nodes/8616788/";
    private final String template;
    
    private TEMPTemplate(String template) {
        this.template = template;
    }
    public String getTemplate() {
        return this.template;
    }
    public byte[] buildTEMP(TEMPValues eflValues, String utilityName, String ratePlanName) throws IOException, DocumentException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfReader reader = new PdfReader(TEMPLATE_URL + this.getTemplate());
        PdfStamper pdfStamper = new PdfStamper(reader, outputStream);
        AcroFields acroFields = pdfStamper.getAcroFields();
        
        SimpleDateFormat format = new SimpleDateFormat("MMMMM dd, yyyy");
        acroFields.setField(TEMPFields.RATENAME_UTILITY.getFieldName(), utilityName + " - " + ratePlanName);
        acroFields.setField(TEMPFields.DATE.getFieldName(), format.format(eflValues.getEffectiveDate().getTime()));
        
        switch (this) {
            case MAT_DOT_UNGREEN_USAGEBASED:
            case MAT_DOT_GREEN_USAGEBASED:
            case VE_DOT_GREEN_USAGEBASED:                
                acroFields.setField(TEMPFields.USAGE.getFieldName(), removeTrailingZero(eflValues.getThreshold()));
                acroFields.setField(TEMPFields.CONTRACT_TERM.getFieldName(), eflValues.getRateTerm().toString());
                acroFields.setField(TEMPFields.ETF.getFieldName(), "$" + removeTrailingZero(eflValues.getEtfFee()));
                break;
        }
        
        pdfStamper.close();
        reader.close();
        return outputStream.toByteArray();
    }
    private String removeTrailingZero(Double value){
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(value);
    }
}

Calling it:

TEMPTemplate.MAT_DOT_GREEN_USAGEBASED.buildTemp(….);

or

TEMPTemplate.valueOf(“MAT_DOT_GREEN_USAGEBASED”).buildTemp(….);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s