/**
   * Import a medicament from one row of the BAG-Medi file
   *
   * @param row
   *     <pre>
   * 		row[0] = ID,bzw Name
   * 		row[1] = Generikum
   * 		row[2] = Pharmacode
   * 	 	row[3] = BAG-Dossier
   * 		row[4] = Swissmedic-Nr
   * 		row[5] = Swissmedic-Liste
   * 	 	row[6]
   * 		row[7] = Namen
   * 		row[8] = EK-Preis
   *      row[9] = VK-Preis
   *      row[10]= Limitatio (Y/N)
   *      row[11]= LimitatioPts
   *      row[12]= Gruppe (optional)
   *      row[13]= Substance (optional)
   * </pre>
   *
   * @return
   */
  public static boolean importUpdate(final String[] row) throws ElexisException {
    String pharmacode = "0";
    BAGMedi imp = null;
    // Kein Pharmacode, dann nach Name suchen
    if (StringTool.isNothing(row[2].trim())) {
      String mid = qbe.findSingle(Artikel.FLD_NAME, "=", row[7]);
      if (mid != null) {
        imp = BAGMedi.load(mid);
      }
    } else {
      try {
        // strip leading zeroes
        int pcode = Integer.parseInt(row[2].trim());
        pharmacode = Integer.toString(pcode);

      } catch (Exception ex) {
        ExHandler.handle(ex);
        log.log(Level.WARNING, "Pharmacode falsch: " + row[2]);
      }

      qbe.clear(true);
      qbe.add(Artikel.FLD_SUB_ID, "=", pharmacode);
      qbe.or();
      qbe.add(Artikel.FLD_SUB_ID, Query.EQUALS, row[2].trim());
      List<Artikel> lArt = qbe.execute();
      if (lArt == null) {
        throw new ElexisException(
            BAGMediImporter.class,
            "Article list was null while scanning for " + pharmacode,
            ElexisException.EE_UNEXPECTED_RESPONSE,
            true);
      }
      if (lArt.size() > 1) {
        // Duplikate entfernen, genau einen gültigen und existierenden Artikel behalten
        Iterator<Artikel> it = lArt.iterator();
        boolean hasValid = false;
        Artikel res = null;
        while (it.hasNext()) {
          Artikel ax = it.next();
          if (hasValid || (!ax.isValid())) {
            if (res == null) {
              res = ax;
            }
            it.remove();
          } else {
            hasValid = true;
          }
        }
        if (!hasValid) {
          if (res != null) {
            if (res.isDeleted()) {
              res.undelete();
              lArt.add(res);
            }
          }
        }
      }
      imp = lArt.size() > 0 ? BAGMedi.load(lArt.get(0).getId()) : null;
    }
    if (imp == null || (!imp.isValid())) {
      imp = new BAGMedi(row[7], pharmacode);

      String sql =
          new StringBuilder()
              .append("INSERT INTO ")
              .append(BAGMedi.EXTTABLE)
              .append(" (ID) VALUES (")
              .append(imp.getWrappedId())
              .append(");")
              .toString();
      PersistentObject.getConnection().exec(sql);

    } else {

      String sql =
          new StringBuilder()
              .append("SELECT ID FROM ")
              .append(BAGMedi.EXTTABLE)
              .append(" WHERE ID=")
              .append(imp.getWrappedId())
              .toString();
      String extid = PersistentObject.getConnection().queryString(sql);
      if (extid == null) {
        sql =
            new StringBuilder()
                .append("INSERT INTO ")
                .append(BAGMedi.EXTTABLE)
                .append(" (ID) VALUES (")
                .append(imp.getWrappedId())
                .append(");")
                .toString();
        PersistentObject.getConnection().exec(sql);
      }
    }
    imp.update(row);
    return true;
  }