Java Source Code: com.sapienter.jbilling.server.util.PreferenceBL


   1: /*
   2: The contents of this file are subject to the Jbilling Public License
   3: Version 1.1 (the "License"); you may not use this file except in
   4: compliance with the License. You may obtain a copy of the License at
   5: http://www.jbilling.com/JPL/
   6: 
   7: Software distributed under the License is distributed on an "AS IS"
   8: basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
   9: License for the specific language governing rights and limitations
  10: under the License.
  11: 
  12: The Original Code is jbilling.
  13: 
  14: The Initial Developer of the Original Code is Emiliano Conde.
  15: Portions created by Sapienter Billing Software Corp. are Copyright 
  16: (C) Sapienter Billing Software Corp. All Rights Reserved.
  17: 
  18: Contributor(s): ______________________________________.
  19: */
  20: 
  21: package com.sapienter.jbilling.server.util;
  22: 
  23: import java.util.Locale;
  24: 
  25: import javax.ejb.CreateException;
  26: import javax.ejb.FinderException;
  27: import javax.naming.NamingException;
  28: 
  29: import org.apache.log4j.Logger;
  30: 
  31: import com.sapienter.jbilling.common.JNDILookup;
  32: import com.sapienter.jbilling.interfaces.PreferenceEntityLocal;
  33: import com.sapienter.jbilling.interfaces.PreferenceEntityLocalHome;
  34: import com.sapienter.jbilling.interfaces.PreferenceTypeEntityLocal;
  35: import com.sapienter.jbilling.interfaces.PreferenceTypeEntityLocalHome;
  36: import com.sapienter.jbilling.server.user.EntityBL;
  37: import com.sapienter.jbilling.server.user.UserBL;
  38: 
  39:	  public class PreferenceBL {
  40:    
  41:
  42:    // private methods
  43:    private JNDILookup EJBFactory = null;
  44:    private PreferenceEntityLocalHome preferenceHome = null;
  45:    private PreferenceTypeEntityLocalHome typeHome = null;
  46:    private PreferenceEntityLocal preference = null;
  47:    private PreferenceTypeEntityLocal type = null;
  48:    private Logger log = null;
  49:    private Locale locale = null;
  50:    
  51:    public PreferenceBL(Integer preferenceId) 
  52:	              throws NamingException, FinderException {
  53:        init();
  54:        preference = preferenceHome.findByPrimaryKey(preferenceId);
  55:    }
  56:    
  57:	      public PreferenceBL() throws NamingException {
  58:        init();
  59:    }
  60:    
  61:	      private void init() throws NamingException {
  62:        log = Logger.getLogger(PreferenceBL.class);             
  63:        EJBFactory = JNDILookup.getFactory(false);
  64:        preferenceHome = (PreferenceEntityLocalHome) 
  65:                EJBFactory.lookUpLocalHome(
  66:                PreferenceEntityLocalHome.class,
  67:                PreferenceEntityLocalHome.JNDI_NAME);
  68:        typeHome = (PreferenceTypeEntityLocalHome) 
  69:                EJBFactory.lookUpLocalHome(
  70:                PreferenceTypeEntityLocalHome.class,
  71:                PreferenceTypeEntityLocalHome.JNDI_NAME);
  72:    }
  73:    
  74:    /**
  75:     * If the entity does not have explicitly set the property
  76:     * it will throw a FinderException, but will still find the
  77:     * defaut.
  78:     * So you need to catch the finder and then use the default
  79:     * @param entityId
  80:     * @param typeId
  81:     * @throws FinderException
  82:     */
  83:    public void set(Integer entityId, Integer typeId) 
  84:	              throws FinderException {
  85:        log.debug("Now looking for preference " + typeId + " ent " +
  86:                entityId + " table " + Constants.TABLE_ENTITY);
  87:	          if (entityId != null) {
  88:	              try {
  89:                EntityBL en = new EntityBL(entityId);
  90:                locale = en.getLocale();
  91:            } catch (Exception e) {}
  92:        }
  93:        
  94:	          try {
  95:            preference = preferenceHome.findByType_Row(
  96:                    typeId, entityId, Constants.TABLE_ENTITY);
  97:        } catch (FinderException e) {
  98:            preference = null;
  99:            type = typeHome.findByPrimaryKey(typeId);
 100:            throw new FinderException(e.getMessage());
 101:        }
 102:            
 103:    }
 104:
 105:    public void setForUser(Integer userId, Integer typeId) 
 106:	              throws FinderException {
 107:	          try {
 108:            UserBL us = new UserBL(userId);
 109:            locale = us.getLocale();
 110:        } catch (Exception e) {}
 111:
 112:	          try {
 113:            preference = preferenceHome.findByType_Row(typeId, userId,
 114:                    Constants.TABLE_BASE_USER);
 115:        } catch (FinderException e) {
 116:            preference = null;
 117:            type = typeHome.findByPrimaryKey(typeId);
 118:            throw new FinderException(e.getMessage());
 119:        }
 120:
 121:    }
 122:
 123:    public void createUpdateForEntity(Integer entityId, Integer preferenceId, 
 124:            Integer intValue, String strValue, Float fValue) 
 125:	              throws CreateException {
 126:        // lets see first if this exists
 127:	          try {
 128:            set(entityId, preferenceId);
 129:            // it does
 130:            preference.setIntValue(intValue);
 131:            preference.setStrValue(strValue);
 132:            preference.setFloatValue(fValue);
 133:        } catch (FinderException e) {
 134:            // we need a new one
 135:            preferenceHome.create(preferenceId, Constants.TABLE_ENTITY, 
 136:                    entityId, intValue, strValue, fValue);
 137:        }
 138:    }
 139:
 140:    public void createUpdateForUser(Integer userId, Integer typeId, 
 141:            Integer intValue, String strValue, Float fValue) 
 142:	              throws CreateException {
 143:        // lets see first if this exists
 144:	          try {
 145:            setForUser(userId, typeId);
 146:            // it does
 147:            preference.setIntValue(intValue);
 148:            preference.setStrValue(strValue);
 149:            preference.setFloatValue(fValue);
 150:        } catch (FinderException e) {
 151:            // we need a new one
 152:            preferenceHome.create(typeId, Constants.TABLE_BASE_USER, 
 153:                    userId, intValue, strValue, fValue);
 154:        }
 155:    }
 156:
 157:	      public int getInt() {
 158:	          if (preference != null) {
 159:            return preference.getIntValue().intValue();
 160:        } 
 161:        
 162:        return type.getIntDefValue().intValue();
 163:    }
 164:
 165:	      public float getFloat() {
 166:	          if (preference != null) {
 167:            return preference.getFloatValue().floatValue();
 168:        } 
 169:        
 170:        return type.getFloatDefValue().floatValue();
 171:    }
 172:
 173:	      public String getString() {
 174:	          if (preference != null) {
 175:            return preference.getStrValue();
 176:        }
 177:        
 178:        return type.getStrDefValue();
 179:    }
 180:    
 181:	      public String getValueAsString() {
 182:	          if (preference.getIntValue() != null) {
 183:            return preference.getIntValue().toString();
 184:        } else if (preference.getStrValue() != null) {
 185:            return preference.getStrValue();
 186:        } else if (preference.getFloatValue() != null) {
 187:	              if (locale == null) {
 188:                return preference.getFloatValue().toString();
 189:            } else {
 190:                return Util.float2string(preference.getFloatValue(), locale);
 191:            }
 192:        }
 193:        
 194:        return null;
 195:    }
 196:    
 197:    public String getDefaultAsString(Integer id) 
 198:	              throws FinderException{
 199:        log.debug("Looking for preference default for type " + id);
 200:        type = typeHome.findByPrimaryKey(id);
 201:	          if (type.getIntDefValue() != null) {
 202:            return type.getIntDefValue().toString();
 203:        } else if (type.getStrDefValue() != null) {
 204:            return type.getStrDefValue();
 205:        } else if (type.getFloatDefValue() != null) {
 206:	              if (locale == null) {
 207:                return  type.getFloatDefValue().toString();
 208:            } else {
 209:                return Util.float2string(type.getFloatDefValue(), locale);
 210:            }
 211:        }
 212:        
 213:        return null;
 214:    }
 215:    
 216:	      public boolean isNull() {
 217:        return preference.getIntValue() == null && 
 218:                preference.getStrValue() == null &&
 219:                preference.getFloatValue() == null;
 220:    }
 221:    
 222:	      public PreferenceEntityLocal getEntity() {
 223:        return preference;
 224:    }
 225:    
 226:}