⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 defaultmotable.java

📁 你个snmp的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      if (row == null) {
        request.getVariableBinding().setVariable(Null.noSuchInstance);
      }
      else if (col != null) {
        col.get(request, row, cell.getColumn());
      }
      else {
        request.getStatus().setErrorStatus(PDU.noAccess);
      }
    }
    else {
      if (cell.getIndex() == null) {
        request.getVariableBinding().setVariable(Null.noSuchInstance);
      }
      else {
        request.getVariableBinding().setVariable(Null.noSuchObject);
      }
    }
    request.completed();
  }

  public boolean next(SubRequest request) {
    DefaultMOScope scope = new DefaultMOScope(request.getScope());
    MOTableCellInfo nextCell;
    while ((nextCell = findCell(scope)) != null) {
      if (columns[nextCell.getColumn()].getAccess().isAccessibleForRead()) {
        Variable value = getValue(nextCell.getIndex(), nextCell.getColumn());
        if (value == null) {
          scope.setLowerBound(nextCell.getCellOID());
          scope.setLowerIncluded(false);
        }
        else {
          request.getVariableBinding().setOid(nextCell.getCellOID());
          request.getVariableBinding().setVariable(value);
          request.completed();
          return true;
        }
      }
      else {
        if (nextCell.getColumn()+1 < getColumnCount()) {
          OID nextColOID = new OID(getOID());
          nextColOID.append(columns[nextCell.getColumn()+1].getColumnID());
          scope.setLowerBound(nextColOID);
          scope.setLowerIncluded(false);
        }
        else {
          return false;
        }
      }
    }
    return false;
  }

  /**
   * prepare
   *
   * @param request SubRequest
   * @throws MOException
   */
  public void prepare(SubRequest request) {
    OID cellOID = request.getVariableBinding().getOid();
    MOTableCellInfo cell = getCellInfo(cellOID);
    if (cell.getIndex() == null) {
      request.getStatus().setErrorStatus(PDU.inconsistentName);
      return;
    }
    if ((cell.getColumn() >= 0) && (cell.getColumn() < columns.length)) {
      MOColumn col = getColumn(cell.getColumn());
      if (logger.isDebugEnabled()) {
        logger.debug("Preparing sub-request ("+request.getVariableBinding()+")"+
                     " for column: "+col);
      }
      if ((col instanceof MOMutableColumn) &&
          (col.getAccess().isAccessibleForWrite())) {
        MOMutableColumn mcol = (MOMutableColumn)col;
        // check index
        if (getIndexDef().isValidIndex(cell.getIndex())) {
          MOTableRow row = model.getRow(cell.getIndex());
          if (row == null) {
            // look for already prepared row
            row = (MOTableRow)
                getNewRows(request.getRequest()).get(cell.getIndex());
          }
          if (row != null) {
            prepare(request, cell, mcol, row, false);
            request.completed();
            return;
          }
          else if (model instanceof MOMutableTableModel) {
            if (logger.isDebugEnabled()) {
              logger.debug("Trying to create new row '"+cell.getIndex()+"'");
            }
            MOMutableTableModel mmodel = (MOMutableTableModel)model;
            // create new row
            try {
              row = createRow(request, cell, mmodel);
              if (row == null) {
                request.getStatus().setErrorStatus(PDU.noCreation);
              }
              else {
                prepare(request, cell, mcol, row, true);
                request.completed();
              }
            }
            catch (UnsupportedOperationException ex) {
              request.getStatus().setErrorStatus(PDU.noCreation);
            }
          }
          else {
            request.getStatus().setErrorStatus(PDU.noCreation);
          }
        }
        else {
          // invalid index
          if (logger.isDebugEnabled()) {
            logger.debug("Invalid index '"+cell.getIndex()+
                         "' for row creation in table "+getID());
          }
          request.getStatus().setErrorStatus(PDU.noCreation);
        }
      }
      else {
        // read-only column
        request.getStatus().setErrorStatus(PDU.notWritable);
      }
    }
    else {
      request.getStatus().setErrorStatus(PDU.noCreation);
    }
  }

  private MOTableRow createRow(SubRequest request, MOTableCellInfo cell,
                               MOMutableTableModel mmodel)
      throws UnsupportedOperationException
  {
    MOColumn col = getColumn(cell.getColumn());
    if (!col.getAccess().isAccessibleForCreate()) {
      // creation not allowed
      return null;
    }
    Variable[] initialValues = new Variable[getColumnCount()];
    getChangesFromRequest(cell.getIndex(), null, request,
                          initialValues, true, true);
    MOTableRow row = mmodel.createRow(cell.getIndex(), initialValues);
    getNewRows(request.getRequest()).put(row.getIndex(), row);
    return row;
  }

  private void prepare(SubRequest request, MOTableCellInfo cell,
                       MOMutableColumn mcol, MOTableRow row, boolean creation) {
    if (moChangeListeners != null) {
      MOChangeEvent changeEvent =
          new MOChangeEvent(this, new CellProxy(cell),
                            cell.getCellOID(),
                            (creation) ? null : row.getValue(cell.getColumn()),
                            request.getVariableBinding().getVariable());
      fireBeforePrepareMOChange(changeEvent);
      if (changeEvent.getDenyReason() != PDU.noError) {
        request.getStatus().setErrorStatus(changeEvent.getDenyReason());
      }
    }
    ChangeSet changeSet = getPendingChangeSet(request, cell.getIndex());
    if (changeSet == null) {
      changeSet = addPendingChanges(request, row, creation);
    }
    if ((moTableRowListeners != null) && (!request.hasError())) {
      if (isChangeSetComplete(request, row.getIndex(), cell.getColumn())) {
        MOTableRowEvent rowEvent =
            new MOTableRowEvent(this, this, row, changeSet, (creation) ?
                                MOTableRowEvent.CREATE :MOTableRowEvent.CHANGE);
        fireRowChanged(rowEvent);
        if (rowEvent.getVetoStatus() != PDU.noError) {
          if (rowEvent.getVetoColumn() >= 0) {
            int colID = columns[rowEvent.getVetoColumn()].getColumnID();
            OID prefix = new OID(getOID());
            prefix.append(colID);
            SubRequest r = request.getRequest().find(prefix);
            if (r != null) {
              r.getStatus().setErrorStatus(rowEvent.getVetoStatus());
            }
            else {
              request.getRequest().setErrorStatus(rowEvent.getVetoStatus());
            }
          }
          else {
            request.getRequest().setErrorStatus(rowEvent.getVetoStatus());
          }
        }
      }
    }
    if (request.getStatus().getErrorStatus() == PDU.noError) {
      mcol.prepare(request, row, changeSet, cell.getColumn());
      MOChangeEvent changeEvent =
          new MOChangeEvent(this, new CellProxy(cell),
                            cell.getCellOID(),
                            row.getValue(cell.getColumn()),
                            request.getVariableBinding().getVariable());
      fireAfterPrepareMOChange(changeEvent);
      if (changeEvent.getDenyReason() != PDU.noError) {
        request.getStatus().setErrorStatus(changeEvent.getDenyReason());
      }
      else {
        if ((row instanceof MOMutableRow2PC) &&
            (isChangeSetComplete(request, row.getIndex(), cell.getColumn()))) {
          ((MOMutableRow2PC)row).prepareRow(request, changeSet);
        }
      }
    }
  }

  protected int getChangesFromRequest(OID index,
                                      MOTableRow row,
                                      SubRequest request,
                                      Variable[] values,
                                      boolean setDefaultValues,
                                      boolean newRow) {
    int lastChangedColumn = -1;
    // assign default values
    if (setDefaultValues) {
      for (int i = 0; (i < values.length) && (i < getColumnCount()); i++) {
        if (columns[i] instanceof MOMutableColumn) {
          values[i] = ((MOMutableColumn) columns[i]).getDefaultValue();
        }
      }
    }
    Request req = request.getRequest();
    for (Iterator it = req.iterator(); it.hasNext();) {
      SubRequest sreq = (SubRequest) it.next();
      OID id = sreq.getVariableBinding().getOid();
      MOTableCellInfo cellInfo = getCellInfo(id);
      if (index.equals(cellInfo.getIndex())) {
        int col = cellInfo.getColumn();
        if ((col >= 0) && (col < values.length)) {
          Variable v = sreq.getVariableBinding().getVariable();
          // check that value is really changed
          if ((v != null) &&
              ((row == null) || (newRow) ||
               (row.size() <= col) ||
               (!v.equals(row.getValue(col))))) {
            values[col] = v;
            lastChangedColumn = col;
          }
        }
      }
    }
    return lastChangedColumn;
  }

  protected boolean hasNewRows(Object key) {
    return ((newRows != null) && (newRows.get(key) != null));
  }

  protected Map getNewRows(Object key) {
    if (newRows == null) {
      newRows = new WeakHashMap(4);
    }
    Map rowMap = (Map) newRows.get(key);
    if (rowMap == null) {
      rowMap = new HashMap(5);
      newRows.put(key, rowMap);
    }
    return rowMap;
  }

  protected synchronized boolean isChangeSetComplete(SubRequest subRequest,
                                                     OID index,
                                                     int column) {
    ChangeSet changeSet = getPendingChangeSet(subRequest, index);
    if (changeSet != null) {
      return (changeSet.getLastChangedColumn() == column);
    }
    return true;
  }

/*
  protected synchronized ChangeSet addPendingChange(SubRequest subRequest,
                                                    OID index, int column,
                                                    Variable value) {
    if (pendingChanges == null) {
      pendingChanges = new WeakHashMap(4);
    }
    Map rowMap = (Map) pendingChanges.get(subRequest.getRequest());
    if (rowMap == null) {
      rowMap = new HashMap(5);
      pendingChanges.put(subRequest.getRequest(), rowMap);
    }
    ChangeSet changeSet = (ChangeSet) rowMap.get(index);
    if (changeSet == null) {
      changeSet = new ChangeSet(index, new Variable[getColumnCount()]);
    }
    changeSet.setValue(column, value);
    return changeSet;
  }
*/

  protected synchronized ChangeSet addPendingChanges(SubRequest subRequest,
                                                     MOTableRow row,
                                                     boolean newRow) {
    if (pendingChanges == null) {
      pendingChanges = new WeakHashMap(4);
    }
    Map rowMap = (Map) pendingChanges.get(subRequest.getRequest());
    if (rowMap == null) {
      rowMap = new HashMap(5);
      pendingChanges.put(subRequest.getRequest(), rowMap);
    }
    Variable[] values = new Variable[getColumnCount()];
    int lastChangedColumn =
        getChangesFromRequest(row.getIndex(), row, subRequest,
                              values, newRow, newRow);
    ChangeSet changeSet = new ChangeSet(row.getIndex(), values);
    changeSet.lastChangedColumn = lastChangedColumn;
    rowMap.put(row.getIndex(), changeSet);
    return changeSet;
  }


  protected ChangeSet getPendingChangeSet(SubRequest subRequest,
                                          OID index) {
    if (pendingChanges != null) {
      Map rowMap = (Map) pendingChanges.get(subRequest.getRequest());
      if (rowMap != null) {
        return (ChangeSet) rowMap.get(index);
      }
    }
    return null;
  }


  public void cleanup(SubRequest request) {
    OID cellOID = request.getVariableBinding().getOid();
    MOTableCellInfo cell = getCellInfo(cellOID);
    if (cell.getIndex() == null) {
      return;
    }
    MOColumn col = getColumn(cell.getColumn());
    if (logger.isDebugEnabled()) {
      logger.debug("Cleaning-up sub-request ("+
                   request.getVariableBinding()+") for column: "+col);
    }
    MOMutableTableRow row = (MOMutableTableRow) model.getRow(cell.getIndex());
    if ((row != null) && (col instanceof MOMutableColumn)) {
      ((MOMutableColumn) col).cleanup(request, row, cell.getColumn());
    }
    if ((row instanceof MOMutableRow2PC) &&
        isChangeSetComplete(request, row.getIndex(), cell.getColumn())) {
      ((MOMutableRow2PC)row).cleanupRow(request,
                                        getPendingChangeSet(request,
          row.getIndex()));
    }
    request.completed();
  }

  public void undo(SubRequest request) {
    OID cellOID = request.getVariableBinding().getOid();
    MOTableCellInfo cell = getCellInfo(cellOID);
    MOMutableColumn col = (MOMutableColumn) getColumn(cell.getColumn());
    if (logger.isDebugEnabled()) {
      logger.debug("Undoing sub-request ("+
                   request.getVariableBinding()+") for column: "+col);
    }
    if (hasNewRows(request.getRequest())) {
      ((MOMutableTableModel)model).removeRow(cell.getIndex());
    }
    else {
      MOMutableTableRow row = (MOMutableTableRow) model.getRow(cell.getIndex());
      if (row != null) {
        col.undo(request, row, cell.getColumn());
      }
      if ((row instanceof MOMutableRow2PC) &&
          isChangeSetComplete(request, row.getIndex(), cell.getColumn())) {
        ((MOMutableRow2PC)row).undoRow(request, getPendingChangeSet(request, row.getIndex()));
      }
    }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -