示例#1
0
 protected String getName() {
   Attribute attribute = findAttribute(type);
   if (attribute != null) {
     return attribute.getKey();
   }
   return name;
 }
示例#2
0
  /**
   * @param newType
   * @param attributeId
   */
  public boolean hasAttributeChanged(DynamicTypeImpl newType, String attributeId) {
    Attribute oldAttribute = findAttributeForId(attributeId);
    Attribute newAttribute = newType.findAttributeForId(attributeId);
    if (oldAttribute == null && newAttribute == null) {
      return false;
    }
    if ((newAttribute == null) || (oldAttribute == null)) {
      return true;
    }
    String newKey = newAttribute.getKey();
    String oldKey = oldAttribute.getKey();
    if (!newKey.equals(oldKey)) {
      return true;
    }
    if (!newAttribute.getType().equals(oldAttribute.getType())) {
      return true;
    }
    {
      String[] keys = newAttribute.getConstraintKeys();
      String[] oldKeys = oldAttribute.getConstraintKeys();
      if (keys.length != oldKeys.length) {
        return true;
      }
      for (int i = 0; i < keys.length; i++) {
        if (!keys[i].equals(oldKeys[i])) return true;
        Object oldConstr = oldAttribute.getConstraint(keys[i]);
        Object newConstr = newAttribute.getConstraint(keys[i]);
        if (oldConstr == null && newConstr == null) continue;
        if (oldConstr == null || newConstr == null) return true;

        if (!oldConstr.equals(newConstr)) return true;
      }
    }
    return false;
  }
示例#3
0
 @Override
 public Iterable<ReferenceInfo> getReferenceInfo() {
   List<ReferenceInfo> result = new ArrayList<ReferenceInfo>();
   String parentId = getParentId();
   result.add(new ReferenceInfo(parentId, DynamicType.class));
   DynamicTypeImpl type = getType();
   for (Map.Entry<String, List<String>> entry : data.entrySet()) {
     String key = entry.getKey();
     Attribute attribute = type.getAttribute(key);
     if (attribute == null) {
       continue;
     }
     Class<? extends Entity> refType = attribute.getRefType();
     if (refType == null) {
       continue;
     }
     List<String> values = entry.getValue();
     if (values != null) {
       for (String value : values) {
         result.add(new ReferenceInfo(value, refType));
       }
     }
   }
   return result;
 }
示例#4
0
 public Attribute getFirstAttributeWithAnnotation(String annotationKey) {
   for (Attribute attribute : attributes) {
     String annotation = attribute.getAnnotation(annotationKey);
     if (annotation != null && annotation.equals("true")) {
       return attribute;
     }
   }
   return null;
 }
示例#5
0
      @Override
      public String getRepresentation(ParseContext context) {

        Attribute attribute = type.findAttributeForId(id);
        if (attribute != null) {
          return attribute.getKey();
        }
        return "";
      }
示例#6
0
 @Override
 public void addEntity(Entity entity) {
   Attribute attribute = (Attribute) entity;
   attributes.add((AttributeImpl) attribute);
   if (attribute.getDynamicType() != null && !this.isIdentical(attribute.getDynamicType()))
     throw new IllegalStateException(
         "Attribute '"
             + attribute
             + "' belongs to another dynamicType :"
             + attribute.getDynamicType());
   ((AttributeImpl) attribute).setParent(this);
 }
示例#7
0
 public Classification newClassification(Classification original) {
   if (!isReadOnly()) {
     throw new IllegalStateException(
         "You can only create Classifications from a persistant Version of DynamicType");
   }
   final ClassificationImpl newClassification = (ClassificationImpl) newClassification(true);
   {
     Attribute[] attributes = original.getAttributes();
     for (int i = 0; i < attributes.length; i++) {
       Attribute originalAttribute = attributes[i];
       String attributeKey = originalAttribute.getKey();
       Attribute newAttribute = newClassification.getAttribute(attributeKey);
       Object defaultValue = originalAttribute.defaultValue();
       Object originalValue = original.getValue(attributeKey);
       if (newAttribute != null && newAttribute.getType().equals(originalAttribute.getType())) {
         Object newDefaultValue = newAttribute.defaultValue();
         // If the default value of the new type differs from the old one and the value is the same
         // as the old default then use the new default
         if (newDefaultValue != null
             && ((defaultValue == null && originalValue == null)
                 || (defaultValue != null
                     && originalValue != null
                     && !newDefaultValue.equals(defaultValue)
                     && (originalValue.equals(defaultValue))))) {
           newClassification.setValue(newAttribute, newDefaultValue);
         } else {
           newClassification.setValue(newAttribute, newAttribute.convertValue(originalValue));
         }
       }
     }
     return newClassification;
   }
 }
示例#8
0
 public void addRefValue(Attribute attribute, ReferenceInfo info) throws RaplaException {
   if (info == null) {
     return;
   }
   if (attribute.getRefType() != info.getType()) {
     throw new RaplaException(
         "Different reference type exepcted "
             + attribute.getRefType()
             + " but was "
             + info.getType());
   }
   String attributeKey = attribute.getKey();
   final String id = info.getId();
   addValue(attributeKey, id);
 }
示例#9
0
 public Classification newClassificationWithoutCheck(boolean useDefaults) {
   final ClassificationImpl classification = new ClassificationImpl(this);
   if (resolver != null) {
     classification.setResolver(resolver);
   }
   // Array could not be up todate
   final Attribute[] attributes2 = getAttributes();
   if (useDefaults) {
     for (Attribute att : attributes2) {
       final Object defaultValue = att.defaultValue();
       if (defaultValue != null) {
         classification.setValue(att, defaultValue);
       }
     }
   }
   return classification;
 }
示例#10
0
  public boolean needsChange(DynamicType newType) {
    if (!hasType(newType)) {
      return false;
    }
    DynamicTypeImpl type = getType();
    if (!newType.getKey().equals(type.getKey())) return true;

    for (String key : data.keySet()) {
      Attribute attribute = getType().getAttribute(key);
      if (attribute == null) {
        return true;
      }
      String attributeId = attribute.getId();
      if (type.hasAttributeChanged((DynamicTypeImpl) newType, attributeId)) return true;
    }
    return false;
  }
示例#11
0
 public <T> void addValue(Attribute attribute, T value) {
   checkWritable();
   String attributeKey = attribute.getKey();
   String stringValue = toStringValue(attribute, value);
   if (stringValue == null) {
     return;
   }
   addValue(attributeKey, stringValue);
 }
示例#12
0
 /**
  * returns the string representation of the given value. if attribute is a reference then the id
  * of the referenced object is returned.
  */
 private String toStringValue(Attribute attribute, Object value) {
   String stringValue = null;
   Class<? extends Entity> refType = attribute.getRefType();
   AttributeType attributeType = attribute.getType();
   if (refType != null) {
     if (value instanceof Entity && ((Entity) value).getTypeClass() == refType) {
       stringValue = ((Entity) value).getId();
     } else {
       throw new IllegalArgumentException(
           "entity expected. but id used please use addRefValue instead of addValue in reading");
     }
   } else if (attributeType.equals(AttributeType.DATE)) {
     return new SerializableDateTimeFormat().formatDate((Date) value);
   } else if (value != null) {
     stringValue = value.toString();
   }
   return stringValue;
 }
示例#13
0
 @Override
 public void replace(ReferenceInfo origId, ReferenceInfo newId) {
   final Set<Entry<String, List<String>>> entrySet = data.entrySet();
   for (Entry<String, List<String>> entry : entrySet) {
     final String attributeKey = entry.getKey();
     final Attribute attribute = getAttribute(attributeKey);
     if (attribute.getRefType() == Allocatable.class) {
       final List<String> list = entry.getValue();
       final String origIdString = origId.getId();
       if (list.contains(origIdString)) {
         list.remove(origIdString);
         final String newIdString = newId.getId();
         if (!list.contains(newIdString)) {
           list.add(newIdString);
         }
       }
     }
   }
 }
 private void printClassificationFilterRule(ClassificationFilterRule rule)
     throws IOException, RaplaException {
   Attribute attribute = rule.getAttribute();
   Assert.notNull(attribute);
   String[] operators = rule.getOperators();
   Object[] values = rule.getValues();
   openTag("rapla:rule");
   att("attribute", attribute.getKey());
   closeTag();
   for (int i = 0; i < operators.length; i++) {
     openTag("rapla:orCond");
     att("operator", operators[i]);
     closeTagOnLine();
     if (values[i] != null) printAttributeValue(attribute, values[i]);
     closeElementOnLine("rapla:orCond");
     println();
   }
   closeElement("rapla:rule");
 }
示例#15
0
  public void commitChange(DynamicType type) {
    if (!hasType(type)) {
      return;
    }

    Collection<String> removedKeys = new ArrayList<String>();
    Map<Attribute, Attribute> attributeMapping = new HashMap<Attribute, Attribute>();
    for (String key : data.keySet()) {
      Attribute attribute = getType().getAttribute(key);
      Attribute attribute2 = type.getAttribute(key);
      // key now longer availabe so remove it
      if (attribute2 == null) {
        removedKeys.add(key);
      }
      if (attribute == null) {
        continue;
      }
      String attId = attribute.getId();
      Attribute newAtt = findAttributeById(type, attId);
      if (newAtt != null) {
        attributeMapping.put(attribute, newAtt);
      }
    }
    for (Attribute attribute : attributeMapping.keySet()) {
      Collection<Object> convertedValues = new ArrayList<Object>();
      Collection<?> valueCollection = getValues(attribute);
      Attribute newAttribute = attributeMapping.get(attribute);
      for (Object oldValue : valueCollection) {
        Object newValue = newAttribute.convertValue(oldValue);
        if (newValue != null) {
          convertedValues.add(newValue);
        }
      }
      setValues(newAttribute, convertedValues);
    }

    for (String key : removedKeys) {
      data.remove(key);
    }
    this.type = type.getKey();
    name = null;
  }
示例#16
0
 public Collection<String> getValuesUnresolvedStrings(Attribute attribute) {
   if (attribute == null) {
     throw new NullPointerException("Attribute can't be null");
   }
   String attributeKey = attribute.getKey();
   // first lookupDeprecated in attribute map
   List<String> list = data.get(attributeKey);
   if (list == null || list.size() == 0) {
     return Collections.emptyList();
   }
   return list;
 }
示例#17
0
 public String getValueUnresolvedString(Attribute attribute) {
   if (attribute == null) {
     throw new NullPointerException("Attribute can't be null");
   }
   String attributeKey = attribute.getKey();
   // first lookupDeprecated in attribute map
   List<String> o = data.get(attributeKey);
   if (o == null || o.size() == 0) {
     return null;
   }
   String stringRep = o.get(0);
   return stringRep;
 }
示例#18
0
 public String toString() {
   try {
     StringBuilder builder = new StringBuilder();
     boolean first = true;
     builder.append("{");
     for (Attribute attribute : getAttributes()) {
       if (!first) {
         builder.append(", ");
       } else {
         first = false;
       }
       String key = attribute.getKey();
       String valueAsString = getValueAsString(attribute, null);
       builder.append(key);
       builder.append(':');
       builder.append(valueAsString);
     }
     builder.append("}");
     return builder.toString();
   } catch (Exception ex) {
     return data.toString();
   }
 }
示例#19
0
 private Object fromString(Attribute attribute, EntityResolver resolver, String value)
     throws EntityNotFoundException, IllegalStateException {
   Class<? extends Entity> refType = attribute.getRefType();
   if (refType != null) {
     Entity resolved = resolver.resolve(value, refType);
     return resolved;
   }
   try {
     Object result = AttributeImpl.parseAttributeValueWithoutRef(attribute, value);
     return result;
   } catch (RaplaException exception) {
     throw new IllegalStateException(exception.getMessage(), exception);
   }
 }
示例#20
0
 public <T> void addValue(Attribute attribute, T value) {
   checkWritable();
   String attributeKey = attribute.getKey();
   String stringValue = ((AttributeImpl) attribute).toStringValue(value);
   if (stringValue == null) {
     return;
   }
   List<String> l = data.get(attributeKey);
   if (l == null) {
     l = new ArrayList<String>();
     data.put(attributeKey, l);
   }
   l.add(stringValue);
 }
示例#21
0
 public void removeAttribute(Attribute attribute) {
   checkWritable();
   String matchingAttributeKey = findAttribute(attribute);
   if (matchingAttributeKey == null) {
     return;
   }
   attributes.remove(attribute);
   if (this.equals(attribute.getDynamicType())) {
     if (((AttributeImpl) attribute).isReadOnly()) {
       throw new IllegalArgumentException(
           "Attribute is not writable. It does not belong to the same dynamictype instance");
     }
     ((AttributeImpl) attribute).setParent(null);
   }
 }
示例#22
0
 public <T> void setValues(Attribute attribute, Collection<T> values) {
   checkWritable();
   String attributeKey = attribute.getKey();
   if (values == null || values.isEmpty()) {
     data.remove(attributeKey);
     name = null;
     return;
   }
   ArrayList<String> newValues = new ArrayList<String>();
   for (Object value : values) {
     String stringValue = ((AttributeImpl) attribute).toStringValue(value);
     if (stringValue != null) {
       newValues.add(stringValue);
     }
   }
   data.put(attributeKey, newValues);
   // isNameUpToDate = false;
   name = null;
 }
示例#23
0
 public Object getValue(Attribute attribute) {
   if (attribute == null) {
     throw new NullPointerException("Attribute can't be null");
   }
   String attributeKey = attribute.getKey();
   // first lookupDeprecated in attribute map
   List<String> o = data.get(attributeKey);
   if (o == null || o.size() == 0) {
     return null;
   }
   String stringRep = o.get(0);
   Object fromString;
   try {
     fromString = ((AttributeImpl) attribute).fromString(resolver, stringRep);
     return fromString;
   } catch (EntityNotFoundException e) {
     throw new IllegalStateException(e.getMessage());
   }
 }
示例#24
0
 public Collection<Object> getValues(Attribute attribute) {
   if (attribute == null) {
     throw new NullPointerException("Attribute can't be null");
   }
   String attributeKey = attribute.getKey();
   // first lookupDeprecated in attribute map
   List<String> list = data.get(attributeKey);
   if (list == null || list.size() == 0) {
     return Collections.emptyList();
   }
   List<Object> result = new ArrayList<Object>();
   for (String value : list) {
     Object obj;
     try {
       obj = ((AttributeImpl) attribute).fromString(resolver, value);
       result.add(obj);
     } catch (EntityNotFoundException e) {
     }
   }
   return result;
 }
示例#25
0
    public void actionPerformed(ActionEvent arg0) {
      try {
        JPanel test = new JPanel();
        test.setLayout(new BorderLayout());
        JPanel content = new JPanel();
        GridLayout layout = new GridLayout();
        layout.setColumns(2);
        layout.setHgap(5);
        layout.setVgap(5);

        // content.setLayout(new TableLayout(new
        // double[][]{{TableLayout.PREFERRED,5,TableLayout.PREFERRED},{TableLayout.PREFERRED,5,TableLayout.PREFERRED,5,TableLayout.PREFERRED, 5, TableLayout.PREFERRED}}));
        content.setLayout(layout);
        test.add(new JLabel(getString("enter_name")), BorderLayout.NORTH);
        test.add(content, BorderLayout.CENTER);
        User user = getUserModule().getUser();

        Allocatable person = user.getPerson();
        JTextField inputSurname = new JTextField();
        addCopyPaste(inputSurname);
        JTextField inputFirstname = new JTextField();
        addCopyPaste(inputFirstname);
        JTextField inputTitle = new JTextField();
        addCopyPaste(inputTitle);
        // Person connected?
        if (person != null) {
          Classification classification = person.getClassification();
          DynamicType type = classification.getType();
          Map<String, JTextField> map = new LinkedHashMap<String, JTextField>();
          map.put("title", inputTitle);
          map.put("firstname", inputFirstname);
          map.put("forename", inputFirstname);
          map.put("surname", inputSurname);
          map.put("lastname", inputSurname);
          int rows = 0;
          for (Map.Entry<String, JTextField> entry : map.entrySet()) {
            String fieldName = entry.getKey();
            Attribute attribute = type.getAttribute(fieldName);
            JTextField value = entry.getValue();
            if (attribute != null && !content.isAncestorOf(value)) {
              Locale locale = getLocale();
              content.add(new JLabel(attribute.getName(locale)));
              content.add(value);
              Object value2 = classification.getValue(attribute);
              rows++;
              if (value2 != null) {
                value.setText(value2.toString());
              }
            }
          }
          layout.setRows(rows);
        } else {
          content.add(new JLabel(getString("name")));
          content.add(inputSurname);
          inputSurname.setText(user.getName());
          layout.setRows(1);
        }
        DialogUI dlg =
            DialogUI.create(
                getContext(),
                getComponent(),
                true,
                test,
                new String[] {getString("save"), getString("abort")});
        dlg.start();
        if (dlg.getSelectedIndex() == 0) {
          String title = inputTitle.getText();
          String firstname = inputFirstname.getText();
          String surname = inputSurname.getText();
          getUserModule().changeName(title, firstname, surname);

          nameLabel.setText(user.getName());
        }
      } catch (RaplaException ex) {
        showException(ex, getMainComponent());
      }
    }
示例#26
0
 AttributeFunction(Attribute attribute) {
   super("attribute:" + attribute.getKey());
   id = attribute.getId();
 }