Esempio n. 1
0
  /**
   * Sets the element name.
   *
   * @param name the new name.
   * @throws NameException if the element name is not allowed to change.
   */
  public void setName(String name) throws NameException {
    name = StringUtil.trimString(name);

    // Ignore change to the current name.

    String oldName = element.getName();
    if ((name == null && oldName == null)
        || (name != null && oldName != null && name.equals(oldName))) return;

    // ignore change to the dimension that refers a shared dimension
    Dimension sharedDimension =
        TabularDimensionPropSearchStrategy.getSharedDimension(module, element);
    if (sharedDimension != null && !sharedDimension.getName().equals(name)) return;

    checkName(name);

    DesignElement tmpElement = element;
    // if a contentElement is not local, need to make a local copy before changing the name
    if (tmpElement instanceof ContentElement && !((ContentElement) tmpElement).isLocal()) {
      eventTarget = ((ContentElement) tmpElement).getValueContainer();
      tmpElement = copyTopCompositeValue();
    }

    // Record the change.

    ActivityStack stack = getActivityStack();
    NameRecord rename = new NameRecord(tmpElement, name);
    stack.startTrans(rename.getLabel());

    // Drop the old name from the name space.

    // Change the name.
    stack.execute(rename);

    // Add the new name to the name space.
    renameSymbolFrom(oldName);

    // change the name of the dimension that shares this element
    if (element instanceof Dimension) {
      updateDimensions(stack);
    }

    stack.commit();
  }
Esempio n. 2
0
  /**
   * Creates unique result set column names if column names are <code>null</code> or empty string.
   *
   * @param resultSetColumn a list containing result set columns
   */
  static void createUniqueResultSetColumnNames(List<ResultSetColumnInfo> columnInfo) {
    if (columnInfo == null || columnInfo.isEmpty()) return;

    Set<String> names = new HashSet<String>();
    for (int i = 0; i < columnInfo.size(); i++) {
      ResultSetColumnInfo tmpInfo = columnInfo.get(i);
      OdaResultSetColumn column = tmpInfo.column;

      String nativeName = column.getNativeName();
      if (nativeName != null) names.add(nativeName);
    }

    Set<String> newNames = new HashSet<String>();
    for (int i = 0; i < columnInfo.size(); i++) {
      ResultSetColumnInfo tmpInfo = columnInfo.get(i);

      OdaResultSetColumn column = tmpInfo.column;
      String nativeName = column.getNativeName();
      String name = column.getColumnName();

      if (!StringUtil.isBlank(name)) {
        newNames.add(name);
        continue;
      }

      nativeName = StringUtil.trimString(nativeName);

      String newName = IdentifierUtility.getUniqueColumnName(names, newNames, nativeName, i);

      newNames.add(newName);
      column.setColumnName(newName);
      if (tmpInfo.hint != null) tmpInfo.hint.setProperty(ColumnHint.COLUMN_NAME_MEMBER, newName);
    }

    names.clear();
    newNames.clear();
  }
Esempio n. 3
0
  /*
   * (non-Javadoc)
   *
   * @see
   * org.eclipse.birt.report.model.core.namespace.INameHelper#getUniqueName
   * (org.eclipse.birt.report.model.core.DesignElement, java.lang.String)
   */
  public String getUniqueName(int namespaceId, DesignElement element, String namePrefix) {
    if (element == null) return null;

    ElementDefn eDefn = (ElementDefn) element.getDefn();

    String name = element.getName();
    if (StringUtil.isBlank(name)) {
      // Use the given prefix if the element name is null
      name = namePrefix;
    }

    name = StringUtil.trimString(name);
    // replace all the illegal chars with '_'
    name = NamePropertyType.validateName(name);

    // Some elements can have a blank name.
    if (eDefn.getNameOption() == MetaDataConstants.NO_NAME) return null;

    if (eDefn.getNameOption() == MetaDataConstants.OPTIONAL_NAME
        && name == null
        && module instanceof ReportDesign) return null;

    if (module instanceof Library
        && element instanceof StyleElement
        && element.getContainer() == null
        && name != null) {
      return name;
    }

    // If the element already has a unique name, return it.
    NameSpace nameSpace = getCachedNameSpace(namespaceId);
    List<String> cachedContentNames = getCachedContentNames(namespaceId);
    NameSpace moduleNameSpace = nameContexts[namespaceId].getNameSpace();

    String validName = name;
    if (element instanceof StyleElement)
      validName = validName == null ? null : validName.toLowerCase();
    if (validName != null
        && isValidInNameSpace(nameSpace, element, validName)
        && isValidInNameSpace(moduleNameSpace, element, validName)
        && !cachedContentNames.contains(validName)) return name;

    // If the element has no name, create it as "New<new name>" where
    // "<new name>" is the new element display name for the element. Both
    // "New" and the new element display name are localized to the user's
    // locale.

    if (name == null) {
      // When creating a new report element which requires a name, the
      // default name will be "New" followed by the element name, such as
      // "New Label"; also, if "NewLabel" already exists, then a number
      // will be appended, such as "NewLabel1", etc.

      if (element instanceof ExtendedItem) {
        ExtensionElementDefn extDefn = ((ExtendedItem) element).getExtDefn();

        PeerExtensionElementDefn peerDefn = (PeerExtensionElementDefn) extDefn;
        IReportItemFactory peerFactory = peerDefn.getReportItemFactory();

        assert peerFactory != null;

        String extensionDefaultName = null;
        IMessages msgs = peerFactory.getMessages();
        if (msgs != null)
          extensionDefaultName =
              msgs.getMessage((String) extDefn.getDisplayNameKey(), module.getLocale());

        if (StringUtil.isBlank(extensionDefaultName)) extensionDefaultName = peerDefn.getName();

        name = ModelMessages.getMessage(MessageConstants.NAME_PREFIX_NEW_MESSAGE);

        name = name + extensionDefaultName;

      } else {
        name =
            ModelMessages.getMessage(
                "New." //$NON-NLS-1$
                    + element.getDefn().getName());
        name = name.trim();
      }
    }

    // Add a numeric suffix that makes the name unique.

    int index = 0;
    String baseName = name;
    validName = name;
    if (element instanceof StyleElement)
      validName = validName == null ? null : validName.toLowerCase();
    while (nameSpace.contains(validName)
        || moduleNameSpace.contains(validName)
        || cachedContentNames.contains(validName)) {
      name = baseName + ++index;
      validName = name;
      if (element instanceof StyleElement)
        validName = validName == null ? null : validName.toLowerCase();
    }

    return name;
  }