/** Tests the hash code computation. */ @Test public void testHashCode() { final CitationImpl citation = new CitationImpl(); final PropertyAccessor accessor = createPropertyAccessor(citation); int hashCode = accessor.hashCode(citation); assertEquals("Empty metadata.", 0, hashCode); final String ISBN = "Dummy ISBN"; citation.setISBN(ISBN); hashCode = accessor.hashCode(citation); assertEquals("Metadata with a single String value.", ISBN.hashCode(), hashCode); final Set<Object> set = new HashSet<Object>(); assertEquals("By Set.hashCode() contract.", 0, set.hashCode()); assertTrue(set.add(ISBN)); assertEquals("Expected Metadata.hashCode() == Set.hashCode().", set.hashCode(), hashCode); final InternationalString title = new SimpleInternationalString("Dummy title"); citation.setTitle(title); hashCode = accessor.hashCode(citation); assertEquals("Metadata with two values.", ISBN.hashCode() + title.hashCode(), hashCode); assertTrue(set.add(title)); assertEquals("Expected Metadata.hashCode() == Set.hashCode().", set.hashCode(), hashCode); assertEquals("CitationsImpl.hashCode() should delegate.", hashCode, citation.hashCode()); final Collection<Object> values = citation.asMap().values(); assertEquals(hashCode, new HashSet<Object>(values).hashCode()); assertTrue(values.containsAll(set)); assertTrue(set.containsAll(values)); }
/** Tests the get method. */ @Test public void testGet() { Citation citation = Citations.EPSG; final PropertyAccessor accessor = createPropertyAccessor(citation); final int index = accessor.indexOf("identifiers"); assertTrue(index >= 0); final Object identifiers = accessor.get(index, citation); assertNotNull(identifiers); assertTrue(containsEPSG(identifiers)); }
/** Tests the constructor. */ @Test public void testConstructor() { final Citation citation = Citations.EPSG; PropertyAccessor accessor; assertNull( "No dummy interface expected.", PropertyAccessor.getType(citation.getClass(), "org.opengis.dummy")); accessor = createPropertyAccessor(citation); assertTrue("Count of 'get' methods.", accessor.count() >= 13); }
/** * Copies all metadata from source to target. The source must implements the same metadata * interface than the target. * * @param source The metadata to copy. * @param target The target metadata. * @param skipNulls If {@code true}, only non-null values will be copied. * @throws ClassCastException if the source or target object don't implements a metadata interface * of the expected package. * @throws UnmodifiableMetadataException if the target metadata is unmodifiable, or if at least * one setter method was required but not found. * @see AbstractMap#AbstractMap(Object) */ public void shallowCopy(final Object source, final Object target, final boolean skipNulls) throws ClassCastException, UnmodifiableMetadataException { ensureNonNull("target", target); final PropertyAccessor accessor = getAccessor(target.getClass()); if (!accessor.type.isInstance(source)) { ensureNonNull("source", source); throw new ClassCastException( Errors.format(ErrorKeys.ILLEGAL_CLASS_$2, source.getClass(), accessor.type)); } if (!accessor.shallowCopy(source, target, skipNulls)) { throw new UnmodifiableMetadataException(Errors.format(ErrorKeys.UNMODIFIABLE_METADATA)); } }
/** * Compares the two specified metadata objects. The comparaison is <cite>shallow</cite>, i.e. all * metadata attributes are compared using the {@link Object#equals} method without recursive call * to this {@code shallowEquals(...)} method for child metadata. * * <p>This method can optionaly excludes null values from the comparaison. In metadata, null value * often means "don't know", so in some occasion we want to consider two metadata as different * only if an attribute value is know for sure to be different. * * <p>The first arguments must be an implementation of a metadata interface, otherwise an * exception will be thrown. The two argument do not need to be the same implementation however. * * @param metadata1 The first metadata object to compare. * @param metadata2 The second metadata object to compare. * @param skipNulls If {@code true}, only non-null values will be compared. * @return {@code true} if the given metadata objects are equals. * @throws ClassCastException if at least one metadata object don't implements a metadata * interface of the expected package. * @see AbstractMetadata#equals */ public boolean shallowEquals( final Object metadata1, final Object metadata2, final boolean skipNulls) throws ClassCastException { if (metadata1 == metadata2) { return true; } if (metadata1 == null || metadata2 == null) { return false; } final PropertyAccessor accessor = getAccessor(metadata1.getClass()); if (!accessor.type.equals(getType(metadata2.getClass()))) { return false; } return accessor.shallowEquals(metadata1, metadata2, skipNulls); }
@Override public String getName() { String res = thisGetName(); if (parent != null) res = parent.getName() + " " + res; return res; }
@Override public String getIdentifier() { String identifier = getAnnotationSource().getAnnotation(ConfXMLTag.class).value(); if (parent != null) identifier = parent.getIdentifier() + "." + identifier; return identifier; }
/** * Returns the target types that need to have conversion. The types contain first as many * constructor parameter types as we have and then the types of properties of object as given by * names of result-set. */ @NotNull private static Optional<List<Type>> findTargetTypes( @NotNull Constructor<?> ctor, @NotNull List<String> resultSetColumns) { List<Type> constructorParameterTypes = asList(ctor.getGenericParameterTypes()); int constructorParameterCount = constructorParameterTypes.size(); if (constructorParameterCount > resultSetColumns.size()) { // We don't have enough columns in ResultSet to instantiate this constructor, discard it. return Optional.empty(); } else if (constructorParameterCount == resultSetColumns.size()) { // We have exactly enough column in ResultSet. Use the constructor as it is. return Optional.of(constructorParameterTypes); } else { // Get the types of remaining properties ArrayList<Type> result = new ArrayList<>(resultSetColumns.size()); result.addAll(constructorParameterTypes); List<String> propertyNames = resultSetColumns.subList(constructorParameterCount, resultSetColumns.size()); for (String name : propertyNames) { Type type = PropertyAccessor.findPropertyType(ctor.getDeclaringClass(), name).orElse(null); if (type != null) result.add(type); else return Optional.empty(); } return Optional.of(result); } }
@Override public int hashCode() { int code = 0; if (parent != null) code = parent.hashCode(); code ^= thisHashCode(); return code; }
@Override public boolean equals(Object obj) { if (obj instanceof PropertyAccessorBase) { PropertyAccessorBase o = (PropertyAccessorBase) obj; if (parent == null && o.parent == null) return true; if (parent != null && o.parent != null) return parent.equals(o.parent); } return false; }
@NotNull private static PropertyAccessor createAccessor( int index, @NotNull Class<?> cl, @NotNull List<String> names) { return PropertyAccessor.findAccessor(cl, names.get(index)) .orElseThrow( () -> new InstantiationFailureException( "Could not find neither setter nor field for '" + names.get(index) + '\'')); }
public ExpirationConfiguration loadExpirationConfiguration() { ExpirationConfiguration result = null; log.debug("begin loadExpirationConfiguration()"); String propertyDir = PropertyAccessor.getPropertyFileLocation(); log.debug("Property Directory: " + propertyDir); result = loadExpirationConfiguration(propertyDir, FTA_CONFIG_FILE); return result; }
/** Tests the {@code indexOf} and {code name} methods. */ @Test public void testName() { final Citation citation = Citations.EPSG; final PropertyAccessor accessor = createPropertyAccessor(citation); assertEquals("Non-existent property", -1, accessor.indexOf("dummy")); assertEquals("getTitle() property", "title", accessor.name(accessor.indexOf("title"))); assertEquals("getTitle() property", "title", accessor.name(accessor.indexOf("TITLE"))); assertEquals("getISBN() property", "ISBN", accessor.name(accessor.indexOf("ISBN"))); assertNull(accessor.name(-1)); }
private DataElementConcept existingMapping(DataElementConcept dec) { List<String> eager = new ArrayList<String>(); eager.add(EagerConstants.AC_CS_CSI); List<DataElementConcept> l = dataElementConceptDAO.find(dec, eager); if (l.size() == 0) throw new PersisterException( PropertyAccessor.getProperty("dec.existing.error", ConventionUtil.publicIdVersion(dec))); DataElementConcept existingDec = l.get(0); return existingDec; }
void init() { contexts = cadsrModule.getAllContexts(); int contentCount = contextComboBox.getItemCount(); int i = 0; while (i <= contentCount && contentCount != 0) { contextComboBox.removeItemAt(i); contentCount = contextComboBox.getItemCount(); } for (Context _con : contexts) { contextComboBox.addItem(_con.getName()); } contextComboBox.setSelectedItem(PropertyAccessor.getProperty("gme.generate.default.context")); }
@Override public void setAsFloat(Object object, float value) { if (parent != null) object = parent.get(object); thisSetValueFloat(object, value); }
@Override public Object get(Object object) { if (parent != null) object = parent.get(object); return thisGetValue(object); }
@Override public float getAsFloat(Object object) { if (parent != null) object = parent.get(object); return thisGetValueFloat(object); }
public void apply() { if (!modified) return; // uncomment to enable feature // if(inheritedAttributes.isInherited(de)) { // if(!userPrefs.getBoolean("de.over.vd.mapping.warning")) { // DontWarnMeAgainDialog dontWarnDialog = new // DontWarnMeAgainDialog("de.over.vd.mapping.warning"); // } // } modified = false; de.setLongName(tempDE.getLongName()); de.setPublicId(tempDE.getPublicId()); de.setVersion(tempDE.getVersion()); de.setContext(tempDE.getContext()); de.setValueDomain(tempDE.getValueDomain()); fireElementChangeEvent(new ElementChangeEvent(node)); if (tempDE.getDataElementConcept() != null) { if (de.getDataElementConcept().getObjectClass().getPublicId() == null || de.getDataElementConcept().getObjectClass().getPublicId().length() == 0) { JOptionPane.showMessageDialog( null, PropertyAccessor.getProperty("oc.mapping.warning"), "Please note", JOptionPane.INFORMATION_MESSAGE); } de.getDataElementConcept() .getObjectClass() .setPublicId(tempDE.getDataElementConcept().getObjectClass().getPublicId()); de.getDataElementConcept() .getObjectClass() .setVersion(tempDE.getDataElementConcept().getObjectClass().getVersion()); de.getDataElementConcept() .getObjectClass() .setLongName(tempDE.getDataElementConcept().getObjectClass().getLongName()); de.getDataElementConcept() .getProperty() .setPublicId(tempDE.getDataElementConcept().getProperty().getPublicId()); de.getDataElementConcept() .getProperty() .setVersion(tempDE.getDataElementConcept().getProperty().getVersion()); } else { boolean found = false; List<DataElement> des = ElementsLists.getInstance().getElements(DomainObjectFactory.newDataElement()); for (DataElement curDe : des) { if (curDe.getDataElementConcept().getObjectClass() == de.getDataElementConcept().getObjectClass()) if (!StringUtil.isEmpty(curDe.getPublicId())) { found = true; } } if (!found) { de.getDataElementConcept().getObjectClass().setPublicId(null); de.getDataElementConcept().getObjectClass().setVersion(null); } } if (tempDE.getDataElementConcept() != null) { firePropertyChangeEvent(new PropertyChangeEvent(this, ButtonPanel.SWITCH, null, false)); firePropertyChangeEvent(new PropertyChangeEvent(this, ApplyButtonPanel.REVIEW, null, true)); } else { firePropertyChangeEvent(new PropertyChangeEvent(this, ButtonPanel.SWITCH, null, true)); firePropertyChangeEvent(new PropertyChangeEvent(this, ApplyButtonPanel.REVIEW, null, false)); } firePropertyChangeEvent(new PropertyChangeEvent(this, ApplyButtonPanel.SAVE, null, false)); }
@Override public String toString() { String res = thisToString(); if (parent != null) res = parent.toString() + res; return res; }
private Object invoke(Object instance, FieldSpec candidate) { return propertyAccessor.invoke(instance, candidate); }
public PropertyAccessor property() { PropertyAccessor accessor = new PropertyAccessor(); accessor.setContext(context); return accessor; }
/** Tests the shallow equals and copy methods. */ @Test public void testEquals() { Citation citation = Citations.EPSG; final PropertyAccessor accessor = createPropertyAccessor(citation); assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF, true)); assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF, false)); assertTrue(accessor.shallowEquals(citation, Citations.EPSG, false)); citation = new CitationImpl(); assertTrue(accessor.shallowCopy(Citations.EPSG, citation, true)); assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF, true)); assertFalse(accessor.shallowEquals(citation, Citations.GEOTIFF, false)); assertTrue(accessor.shallowEquals(citation, Citations.EPSG, false)); final int index = accessor.indexOf("identifiers"); final Object source = accessor.get(index, Citations.EPSG); final Object target = accessor.get(index, citation); assertNotNull(source); assertNotNull(target); assertNotSame(source, target); assertEquals(source, target); assertTrue(containsEPSG(target)); assertSame(target, accessor.set(index, citation, null)); final Object value = accessor.get(index, citation); assertNotNull(value); assertTrue(((Collection) value).isEmpty()); try { accessor.shallowCopy(citation, Citations.EPSG, true); fail("Citations.EPSG should be unmodifiable."); } catch (UnmodifiableMetadataException e) { // This is the expected exception. } }
@Override public AnnotatedElement getAnnotationSource() { if (parent == null) throw new IllegalArgumentException("No annotation source!"); else return parent.getAnnotationSource(); }
@Override public <T> BeanDescriptor<T> provide(Class<T> ofClass, Type ofType, Genson genson) { Map<String, LinkedList<PropertyMutator>> mutatorsMap = new LinkedHashMap<String, LinkedList<PropertyMutator>>(); Map<String, LinkedList<PropertyAccessor>> accessorsMap = new LinkedHashMap<String, LinkedList<PropertyAccessor>>(); List<BeanCreator> creators = provideBeanCreators(ofType, genson); provideBeanPropertyAccessors(ofType, accessorsMap, genson); provideBeanPropertyMutators(ofType, mutatorsMap, genson); List<PropertyAccessor> accessors = new ArrayList<PropertyAccessor>(accessorsMap.size()); for (Map.Entry<String, LinkedList<PropertyAccessor>> entry : accessorsMap.entrySet()) { PropertyAccessor accessor = checkAndMergeAccessors(entry.getKey(), entry.getValue()); // in case of... if (accessor != null) accessors.add(accessor); } Map<String, PropertyMutator> mutators = new HashMap<String, PropertyMutator>(mutatorsMap.size()); for (Map.Entry<String, LinkedList<PropertyMutator>> entry : mutatorsMap.entrySet()) { PropertyMutator mutator = checkAndMergeMutators(entry.getKey(), entry.getValue()); if (mutator != null) mutators.put(mutator.name, mutator); } BeanCreator ctr = checkAndMerge(ofType, creators); if (ctr != null) mergeAccessorsWithCreatorProperties(ofType, accessors, ctr); if (ctr != null) mergeMutatorsWithCreatorProperties(ofType, mutators, ctr); // 1 - prepare the converters for the accessors for (PropertyAccessor accessor : accessors) { accessor.propertySerializer = provide(accessor, genson); } // 2 - prepare the mutators for (PropertyMutator mutator : mutators.values()) { mutator.propertyDeserializer = provide(mutator, genson); } // 3 - prepare the converters for creator parameters if (ctr != null) { for (PropertyMutator mutator : ctr.parameters.values()) { mutator.propertyDeserializer = provide(mutator, genson); } } // lets fail fast if the BeanDescriptor has been built for the wrong type. // another option could be to pass in all the methods an additional parameter Class<T> that // would not necessarily correspond to the rawClass of ofType. In fact we authorize that // ofType rawClass is different from Class<T>, but the BeanDescriptor must match! BeanDescriptor<T> descriptor = create(ofClass, ofType, ctr, accessors, mutators, genson); if (!ofClass.isAssignableFrom(descriptor.getOfClass())) throw new ClassCastException( "Actual implementation of BeanDescriptorProvider " + getClass() + " seems to do something wrong. Expected BeanDescriptor for type " + ofClass + " but provided BeanDescriptor for type " + descriptor.getOfClass()); return descriptor; }
/** Creates a property accessor for the given citation. */ private static PropertyAccessor createPropertyAccessor(final Citation citation) { final Class<?> implementation = citation.getClass(); final Class<?> type = PropertyAccessor.getType(implementation, "org.opengis.metadata"); assertNotNull(type); return new PropertyAccessor(implementation, type); }
/** Tests the set method. */ @Test public void testSet() { Citation citation = new CitationImpl(); final PropertyAccessor accessor = createPropertyAccessor(citation); // Tries with ISBN, which expect a String. Object value = "Random number"; int index = accessor.indexOf("ISBN"); assertTrue(index >= 0); assertNull(accessor.set(index, citation, value)); assertSame(value, accessor.get(index, citation)); assertSame(value, citation.getISBN()); // Tries with the title. Automatic conversion from String to InternationalString expected. index = accessor.indexOf("title"); assertTrue(index >= 0); assertNull(accessor.set(index, citation, "A random title")); value = accessor.get(index, citation); assertTrue(value instanceof InternationalString); assertEquals("A random title", value.toString()); assertSame(value, citation.getTitle()); // Tries with an element to be added in a collection. index = accessor.indexOf("alternateTitle"); assertTrue(index >= 0); value = accessor.get(index, citation); assertTrue(value instanceof Collection); assertTrue(((Collection) value).isEmpty()); value = accessor.set(index, citation, "An other title"); assertTrue(value instanceof Collection); assertEquals(1, ((Collection) value).size()); value = accessor.set(index, citation, "Yet an other title"); assertTrue(value instanceof Collection); assertEquals(2, ((Collection) value).size()); }
public void persist() { DataElementConcept dec = DomainObjectFactory.newDataElementConcept(); List<DataElementConcept> decs = elements.getElements(dec); logger.debug("***** Inside DEC Persist "); int count = 0; sendProgressEvent(count++, decs.size(), "DECs"); logger.debug("decs... "); if (decs != null) { for (ListIterator<DataElementConcept> it = decs.listIterator(); it.hasNext(); ) { DataElementConcept newDec = DomainObjectFactory.newDataElementConcept(); dec = it.next(); sendProgressEvent(count++, decs.size(), "DEC : " + dec.getLongName()); List<AlternateName> passedAltNames = new ArrayList<AlternateName>(); for (AlternateName _an : dec.getAlternateNames()) passedAltNames.add(_an); List<Definition> modelDefinitions = new ArrayList<Definition>(); for (Definition _def : dec.getDefinitions()) modelDefinitions.add(_def); List<AdminComponentClassSchemeClassSchemeItem> passedACCSCSI = dec.getAcCsCsis(); dec.removeDefinitions(); dec.removeAlternateNames(); if (!StringUtil.isEmpty(dec.getPublicId()) && dec.getVersion() != null) { newDec = existingMapping(dec); dec.setId(newDec.getId()); for (AlternateName _an : passedAltNames) { persisterUtil.addAlternateName(dec, _an); } it.set(newDec); persisterUtil.addPackageClassification(dec); logger.info(PropertyAccessor.getProperty("mapped.to.existing.dec")); continue; } // update object class with persisted one if (dec.getObjectClass().getPublicId() == null) dec.setObjectClass(LookupUtil.lookupObjectClass(dec.getObjectClass().getPreferredName())); else dec.setObjectClass( LookupUtil.lookupObjectClass( dec.getObjectClass().getPublicId(), dec.getObjectClass().getVersion())); newDec.setObjectClass(dec.getObjectClass()); // update property with persisted one if (dec.getProperty().getPublicId() == null) dec.setProperty(LookupUtil.lookupProperty(dec.getProperty().getPreferredName())); else dec.setProperty( LookupUtil.lookupProperty( dec.getProperty().getPublicId(), dec.getProperty().getVersion())); newDec.setProperty(dec.getProperty()); logger.debug("dec name: " + dec.getLongName()); // logger.debug("alt Name: " + ne); List<String> eager = new ArrayList<String>(); eager.add("definitions"); // does this dec exist? List l = dataElementConceptDAO.find(newDec, eager); logger.debug("***** decs size : " + l.size()); if (l.size() == 0) { logger.debug("***** one or more decs "); if (dec.getConceptualDomain() == null) dec.setConceptualDomain(defaults.getConceptualDomain()); dec.setContext(defaults.getContext()); dec.setLongName( dec.getObjectClass().getLongName() + " " + dec.getProperty().getLongName()); dec.setPreferredDefinition( dec.getObjectClass().getPreferredDefinition() + DEC_PREFERRED_DEF_CONCAT_CHAR + dec.getProperty().getPreferredDefinition()); dec.setPreferredName( ConventionUtil.publicIdVersion(dec.getObjectClass()) + DEC_PREFERRED_NAME_CONCAT_CHAR + ConventionUtil.publicIdVersion(dec.getProperty())); dec.setVersion(new Float(1.0f)); dec.setWorkflowStatus(defaults.getWorkflowStatus()); dec.setProperty(LookupUtil.lookupProperty(dec.getProperty().getPreferredName())); dec.setAudit(defaults.getAudit()); dec.setLifecycle(defaults.getLifecycle()); StringBuilder builder = new StringBuilder(); for (char currentChar : dec.getPreferredDefinition().toCharArray()) { Character replacementChar = charReplacementMap.get(currentChar); builder.append(replacementChar != null ? replacementChar : currentChar); } dec.setPreferredDefinition(builder.toString()); logger.debug( "***** preferred name " + dec.getPreferredName() + " public ID " + dec.getPublicId()); newDec = dataElementConceptDAO.create(dec); logger.info(PropertyAccessor.getProperty("created.dec")); } else { newDec = (DataElementConcept) l.get(0); logger.info(PropertyAccessor.getProperty("existed.dec")); /* if DEC alreay exists, check context * If context is different, add Used_by alt_name */ dec.setId(newDec.getId()); if (!newDec.getContext().getId().equals(defaults.getContext().getId())) { AlternateName _an = DomainObjectFactory.newAlternateName(); _an.setName(defaults.getContext().getName()); _an.setType(AlternateName.TYPE_USED_BY); persisterUtil.addAlternateName(dec, _an); } } dec.setId(newDec.getId()); for (AlternateName _an : passedAltNames) { persisterUtil.addAlternateName(dec, _an); } for (Definition def : modelDefinitions) { persisterUtil.addAlternateDefinition(dec, def); } LogUtil.logAc(newDec, logger); logger.info("-- Public ID: " + newDec.getPublicId()); logger.info( PropertyAccessor.getProperty("oc.longName", newDec.getObjectClass().getLongName())); logger.info( PropertyAccessor.getProperty("prop.longName", newDec.getProperty().getLongName())); dec.setAcCsCsis(passedACCSCSI); persisterUtil.addPackageClassification(dec); it.set(newDec); // dec still referenced in DE. Need ID to retrieve it in DEPersister. dec.setId(newDec.getId()); } } logger.debug("***** End DEC persist"); }
/** * Returns the metadata interface implemented by the specified implementation. Only one metadata * interface can be implemented. * * @param metadata The metadata implementation to wraps. * @return The single interface, or {@code null} if none where found. */ private Class<?> getType(final Class<?> implementation) { return PropertyAccessor.getType(implementation, interfacePackage); }