public void importFirmware(
      String manExtID, String modelExtID, String fromVersion, String toVersion, String filename)
      throws Exception {
    ManagementBeanFactory factory = this.getManagementBeanFactory();
    try {
      ModelBean modelBean = factory.createModelBean();
      UpdateImageBean bean = factory.createUpdateImageBean();

      Manufacturer manufacturer = modelBean.getManufacturerByExternalID(manExtID);
      Model model = modelBean.getModelByManufacturerModelID(manufacturer, modelExtID);

      Image image_1 = bean.getImageByVersionID(model, fromVersion);
      if (image_1 == null) {
        image_1 = bean.newImageInstance(model, fromVersion, true, fromVersion);
      }

      Image image_2 = bean.getImageByVersionID(model, toVersion);
      if (image_2 == null) {
        image_2 = bean.newImageInstance(model, toVersion, true, toVersion);
      }

      File file_1_to_2 = new File(filename);
      InputStream in_1_to_2 = new FileInputStream(file_1_to_2);

      // Test case #1
      Update update_1_to_2 = bean.getUpdate(model, fromVersion, toVersion);
      if (update_1_to_2 != null) {
        update_1_to_2.setBinary(in_1_to_2);
      } else {
        update_1_to_2 = bean.newUpdateInstance(image_1, image_2, in_1_to_2);
      }
      update_1_to_2.setStatus(bean.getImageUpdateStatus(Image.STATUS_RELEASED));
      factory.beginTransaction();
      bean.update(update_1_to_2);
      factory.commit();

      this.getSetup().getConsole().println("           [" + fromVersion + " -> " + toVersion + "]");

      //
      // Caution: must release the Hibernate Session, otherwise will throw the
      // floowing exception:
      // java.lang.UnsupportedOperationException: Blob may not be manipulated
      // from creating session
      //
    } catch (Exception e) {
      factory.rollback();
      throw e;
    } finally {
      factory.release();
    }
  }
 /**
  * @param modelBean
  * @param model
  * @return
  * @throws Exception
  */
 protected boolean isSelected(ModelBean modelBean, Model model) throws Exception {
   String v = modelBean.getCapability(model, "j2me_midp_2_0");
   if (StringUtils.isNotEmpty(v) && v.trim().equals("true")) {
     return true;
   }
   return false;
 }
  protected void processFirmwares() throws SetupException {
    List<String> filenames = this.getFilenames();
    ManagementBeanFactory factory = null;
    try {
      factory = this.getManagementBeanFactory();
      ModelBean modelBean = factory.createModelBean();

      // Import Profile Mappings
      for (String filename : filenames) {
        // Process the file, and import data into database.
        File file = new File(filename);
        if (!file.isAbsolute()) {
          file = new File(this.getSetup().getWorkDir(), filename);
        }

        this.getSetup()
            .getConsole()
            .println("         Loading file [ " + file.getAbsolutePath() + " ]");
        List<ManufacturerItem> items = this.loadManufacturerItems(file.getAbsolutePath());
        for (ManufacturerItem manufacturerItem : items) {
          List<ModelItem> modelItems = manufacturerItem.getModels();
          Manufacturer manufacturer =
              modelBean.getManufacturerByExternalID(manufacturerItem.getExternalID());
          for (ModelItem modelItem : modelItems) {
            // Copy information from family
            this.copyFromFamily(modelItem);

            Model model =
                modelBean.getModelByManufacturerModelID(manufacturer, modelItem.getExternalID());

            if (!modelItem.getFirmwares().isEmpty()) {
              this.getSetup()
                  .getConsole()
                  .println(
                      "         * Importing Firmwares for [ "
                          + manufacturer.getExternalId()
                          + " "
                          + model.getManufacturerModelId()
                          + " ]");
            }
            for (FirmwareItem firmwareItem : modelItem.getFirmwares()) {
              File firmwareFile = new File(firmwareItem.getFilename());
              if (!firmwareFile.isAbsolute()) {
                firmwareFile = new File(this.getSetup().getWorkDir(), firmwareItem.getFilename());
              }
              if (!firmwareFile.exists()) {
                this.getSetup()
                    .getConsole()
                    .println("Could not load firmware from: " + firmwareFile.getAbsolutePath());
                continue;
              }
              String fromVersion = firmwareItem.getFromVersion();
              String toVersion = firmwareItem.getToVersion();
              this.importFirmware(
                  manufacturer.getExternalId(),
                  model.getManufacturerModelId(),
                  fromVersion,
                  toVersion,
                  firmwareFile.getAbsolutePath());
            }
          }
        }
      }
    } catch (Exception ex) {
      throw new SetupException("Error in import models.", ex);
    } finally {
      if (factory != null) {
        factory.release();
      }
    }
  }
  protected void processCPTemplates() throws SetupException {
    List<String> filenames = this.getFilenames();
    ManagementBeanFactory factory = null;
    try {
      factory = this.getManagementBeanFactory();
      ModelBean modelBean = factory.createModelBean();
      ClientProvTemplateBean cpBean = factory.createClientProvTemplateBean();

      // Import Profile Mappings
      for (String filename : filenames) {
        // Process the file, and import data into database.
        File file = new File(filename);
        if (!file.isAbsolute()) {
          file = new File(this.getSetup().getWorkDir(), filename);
        }

        this.getSetup()
            .getConsole()
            .println("         Loading file [ " + file.getAbsolutePath() + " ]");
        List<ManufacturerItem> items = this.loadManufacturerItems(file.getAbsolutePath());
        for (ManufacturerItem manufacturerItem : items) {
          List<ModelItem> modelItems = manufacturerItem.getModels();
          Manufacturer manufacturer =
              modelBean.getManufacturerByExternalID(manufacturerItem.getExternalID());
          for (ModelItem modelItem : modelItems) {
            // Copy information from family
            this.copyFromFamily(modelItem);

            Model model =
                modelBean.getModelByManufacturerModelID(manufacturer, modelItem.getExternalID());

            if (!modelItem.getCpTemplatesFiles().isEmpty()) {
              this.getSetup()
                  .getConsole()
                  .println(
                      "         * Importing CP Templates for [ "
                          + manufacturer.getExternalId()
                          + " "
                          + model.getManufacturerModelId()
                          + " ]");
            }
            for (String templateFilename : modelItem.getCpTemplatesFiles()) {
              File templateFile = new File(templateFilename);
              if (!templateFile.isAbsolute()) {
                templateFile = new File(this.getSetup().getWorkDir(), templateFilename);
              }
              InputStream in = new FileInputStream(templateFile);
              List<ClientProvTemplate> templates =
                  cpBean.importClientProvTemplates(in, this.getSetup().getWorkDir());
              factory.beginTransaction();
              for (ClientProvTemplate template : templates) {
                cpBean.attach(model, template);
              }
              factory.commit();
              this.getSetup().getConsole().println("           [ " + templateFile + " ] imported!");
            }
          }
        }
      }
    } catch (Exception ex) {
      if (factory != null) {
        factory.rollback();
      }
      throw new SetupException("Error in import models.", ex);
    } finally {
      if (factory != null) {
        factory.release();
      }
    }
  }
  protected void processModelBasicInformation() throws SetupException {
    List<String> filenames = this.getFilenames();

    ManagementBeanFactory factory = null;
    try {
      factory = this.getManagementBeanFactory();
      ModelBean modelBean = factory.createModelBean();

      if (System.getProperty("otas.dm.home") == null) {
        log.error(
            "ModelTask: "
                + "Could not found 'otas.dm.home' from system properties, please set 'otas.dm.home'.");
      }

      DDFTreeBean ddfBean = factory.createDDFTreeBean();

      // Import Model, DDF, TACs
      for (String filename : filenames) {
        // Process the file, and import data into database.
        File file = new File(filename);
        if (!file.isAbsolute()) {
          file = new File(this.getSetup().getWorkDir(), filename);
        }

        this.getSetup()
            .getConsole()
            .println("         Loading file [ " + file.getAbsolutePath() + " ]");
        List<ManufacturerItem> items = this.loadManufacturerItems(file.getAbsolutePath());
        for (ManufacturerItem manufacturerItem : items) {
          List<ModelItem> modelItems = manufacturerItem.getModels();
          Manufacturer manufacturer =
              modelBean.getManufacturerByExternalID(manufacturerItem.getExternalID());
          for (ModelItem modelItem : modelItems) {
            Model model =
                modelBean.getModelByManufacturerModelID(manufacturer, modelItem.getExternalID());
            if (model == null) {
              log.info(
                  "Create a new model: "
                      + manufacturer.getExternalId()
                      + " "
                      + modelItem.getExternalID());
              model = modelBean.newModelInstance();
              model.setManufacturer(manufacturer);
            } else {
              log.info(
                  "Modify a model: "
                      + manufacturer.getExternalId()
                      + " "
                      + modelItem.getExternalID());
            }

            // Copy information from family
            this.copyFromFamily(modelItem);
            // Copy item into model entity
            this.copy(modelItem, model);

            this.getSetup()
                .getConsole()
                .println(
                    "         * Importing ["
                        + manufacturer.getExternalId()
                        + " "
                        + model.getManufacturerModelId()
                        + "] from [ "
                        + modelItem.getDefinedByFilename()
                        + " ]");
            factory.beginTransaction();
            modelBean.update(model);
            this.getSetup().getConsole().println("           * Basic information processed");

            // ModelSpec: Icon, ...
            String iconFilename = modelItem.getIconFile();
            if (StringUtils.isEmpty(iconFilename)) {
              iconFilename = this.getSetup().getPropertyValue("model.default.icon.file");
            }
            File iconFile = new File(iconFilename);
            if (!iconFile.isAbsolute()) {
              iconFile = new File(this.getSetup().getWorkDir(), iconFilename);
            }
            if (iconFile.exists()) {
              model.setIconBinary(new FileInputStream(iconFile));
            } else {
              this.getSetup()
                  .getConsole()
                  .println("           Could not found icon file: " + iconFile.getAbsolutePath());
            }
            modelBean.update(model);

            Map<String, String> specMap = modelItem.getSpecifications();
            for (String specName : specMap.keySet()) {
              String category = null;
              String name = specName;
              int index = specName.indexOf(".");
              if (index >= 0) {
                category = specName.substring(0, index);
                name = specName.substring(index + 1, specName.length());
              }
              String value = specMap.get(specName);
              List<ModelCharacter> characters =
                  modelBean.findModelCharacters(model, category, name);
              ModelCharacter character = null;
              if (characters != null && characters.size() > 0) {
                // Update exists character
                character = characters.get(0);
              } else {
                // Create a new character
                character = modelBean.newModelCharacterInstance(model, category, name);
              }
              character.setValue(value);
              modelBean.update(character);
            }
            this.getSetup().getConsole().println("           * Specifications processed");

            // TACs
            for (String tac : modelItem.getTacs()) {
              if (StringUtils.isNotEmpty(tac)) {
                modelBean.addTACInfo(model, tac);
                this.getSetup().getConsole().println("           * TAC information processed");
              }
            }

            // DDF Files
            for (String ddfFilename : modelItem.getDdfFiles()) {
              File ddfFile = new File(ddfFilename);
              if (!ddfFile.isAbsolute()) {
                ddfFile = new File(this.getSetup().getWorkDir(), ddfFilename);
              }
              DDFTree ddfTree = null;
              try {
                ddfTree = ddfBean.parseDDFTree(new FileInputStream(ddfFile));
              } catch (Exception e) {
                throw new DMException("Error to parse DDF File: " + ddfFile.getAbsolutePath(), e);
              }
              ddfBean.addDDFTree(ddfTree);
              modelBean.attachDDFTree(model, ddfTree.getID());
              this.getSetup()
                  .getConsole()
                  .println("           * DDF information [ " + ddfFile + " ] processed");
            }
            factory.commit();
          }
        }
      }

    } catch (Exception ex) {
      if (factory != null) {
        factory.rollback();
      }
      throw new SetupException("Error in import models.", ex);
    } finally {
      if (factory != null) {
        factory.release();
      }
    }
  }