Пример #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();
 }