I'm trying to map properties of beans, which are in different packages, using dozer eg:
<mapping>
<class-a>com.naeem.schema.basictypes.Birth</class-a>
<class-b>com.naeem.schema.forms.n840.DateStore</class-b>
<field>
<a>countryOfBirth</a>
<b>countryOfBirth</b>
</field>
</mapping>
is this possible in dozer. Thanks
Yes, Its possible in dozer.
By default, dozer will map all property from source object to same name property in destination object. So in your case both the class has a property countryOfBirth of same name. So you dont even have to write mapping file. Following will be suffice :
DozerMapper mapper = new DozerMapper();
Birth birth = new Birth();
//set different fields of birth object
DateStore dateStore = new DateStore();
mapper.map(birth,dateStore);
Or,ulternatively,
DozerMapper mapper = new DozerMapper();
Birth birth = new Birth();
//set different fields of birth object
DateStore dateStore = mapper.map(birth,DateStore.class);
Related
Trying to create a data binding to custom XML part for a sdtBlock representing a rich text content control is not working because it creates a w:dataBinding tag in the resulting XML instead of a w15:dataBinding. Rich text content control require a w15 namespace uri.
I am creating the data binding directly in Java, not in an authoring tool, with this code:
org.docx4j.wml.CTDataBinding cTDataBinding = new CTDataBinding();
And there is no data binding class in org.docx4j.w15 package.
Is there any way to specify that a different namespace should be used for this instance when marshalling?
https://github.com/plutext/docx4j/blob/master/docx4j-openxml-objects/src/main/java/org/docx4j/w15/ObjectFactory.java#L232 contains:
#XmlElementDecl(namespace = "http://schemas.microsoft.com/office/word/2012/wordml", name = "dataBinding")
public JAXBElement<CTDataBinding> createDataBinding(CTDataBinding value) {
return new JAXBElement<CTDataBinding>(_DataBinding_QNAME, CTDataBinding.class, null, value);
}
so you should be able to create what you want using the w15 ObjectFactory. For example:
SdtPr sdtPr = new SdtPr();
JAXBElement<CTDataBinding> w15DataBinding = new org.docx4j.w15.ObjectFactory().createDataBinding(new CTDataBinding());
sdtPr.getRPrOrAliasOrLock().add(w15DataBinding);
System.out.println(XmlUtils.marshaltoString(sdtPr));
produces (omitting some namespaces):
<w:sdtPr xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" >
<w15:dataBinding/>
</w:sdtPr>
I'm using VB.NET 2013 and am trying to write an application that will import (from csv file) customer contact information (for example: CustomerName, ContactLast, ContactFirst, ContactPhn, ContactEmail). There are going to be multiple csv layouts that have to be dealt with, so I'm using an XML file to store the various mappings in.
I'd like to load the defined maps from DataMaps.xlm into DataMap objects then insert those DataMap objects into a list box. I'd like to have them populated with the XML values before I load them into the listbox. I don't expect that there will ever be more than 10 - 15 defined maps, so loading them right away shouldn't be much of a problem with memory or speed (as it will be a locally run application with locally stored xml data).
What I have so far:
Contact Object (short and sweet for this example - not really needed at this point, but given so you can see how the data map is used)
Private mCompany as String
Private mLastName as String
Private mFirstName as String
Private mPhone as String
Private mEmail As String
<... Support Get/Set items (Properties are same as private variables; minus the leading "m") ...>
DataMap Object (short and sweet again for this example)
Private mDataMapName as String
private mCompany_Col as Integer
private mLastName_Col as Integer
private mFirstName_Col as Integer
private mPhone_Col As Integer
private mEmail_Col As String
<... Support Get/Set items (Properties are same as private variables; minus the leading "m") ...>
"Standard" CSV layout
"Company ABC", "Jones", "June", "515-874-0098", "JJ#ABC.COM"
"Company EFG", "Williams", "Alan", "515-874-2887", "alan#efg.com"
The DataMap for this layout would be:
<Map>
<DataMapName>Standard</DataMapName>
<Company>1</Company>
<LastName>2</LastName>
<FirstName>3</FirstName>
<Phone>4</Phone>
<Email>5</Email>
</Map>
"Not Standard 01" CSV Layout
"Jones", "June", "Company ABC", "515-874-0098", "JJ#ABC.COM"
"Williams", "Alan" "Company EFG", "515-874-2887", "alan#efg.com"
The DataMap for this format would be:
<Map>
<DataMapName>Not Standard 01</DataMapName>
<Company>3</Company>
<LastName>1</LastName>
<FirstName>2</FirstName>
<Phone>4</Phone>
<Email>5</Email>
</Map>
I'm just picking up VB.NET again and am not comfortable with determining the "best" techniques to use while reading data from the .xml file (I can retrieve a list of s using LINQ currently) and populating my DataMap object. I know I can do it by using each individual property to set the values; but I'd like to write generic (looping) code as this app may be rewritten with changes to the DataMap requirements as well as using this in other application instances.
Currently, I am able to create a new element and add it to the DataMaps.xml file. I can then read those changes correctly.
Where I'm stuck is writing a 'generic' populateMap(mapName As String) function. Where I am right now, I can get the map (as an Enumeration of XElement) and then individually look at each property and store it into a new instance of a DataMap object:
Dim dm as New DataMap
dm.DataMapName = map.Element("DataMapName").Value
dm.Company_Col = map.Element("Company_Col").value
dm.LastName_Col = map.Element("LastName_Col").value
dm.FirstName_Col = map.Element("FirstName_Col").value
...
But, I'd like know if it's possible to create something like: (pseudocode)
Dim dm As new DataMap
Dim myInstance As New DataMap
Dim myFields As FieldInfo()
Dim myType As Type = GetType(DataMap)
try
myFields = myType.GetFields(BindingFlags.NonPublic)
for each element in map.Elements
Dim f as FieldInfo = GETType(DataMap).GetField(field.name)
dm.(some sort of reference to a FieldInfo() column name, or something) = element.f
(this would look like: dm.Company_Col = element.Company_Col (or a variable referencing the private value "mCompany_Col")
next
catch ...
Finally ...
End Try
After I execute breeze query as shown below:
var breezeQuery = function(){
var query = EntityQuery.from('TableA')
.inlineCount();
function querySuceeded(data) {
//data.results[0] contains the entity
}
manager.executeQuery(query)
.then(querySuceeded)
}
I get the entity in data.results[0] which contains properties as well as other information like entityAspect etc.
How can I get the property names present in a breeze entity ?
Use the MetadataStore. Something like this:
var tableAType = manager.metadataStore.getEntityType("TableA");
var dataProperties = tableAType.dataProperties;
var navigationProperties = tableAType.navigationProperties;
or from an instance of a entity ( not a projection), since every entity will have an 'entityType' property you can also do this:
var tableAType = tableAInstance.entityType;
var dataProperties = tableAType.dataProperties;
var navigationProperties = tableAType.navigationProperties;
Also see: http://www.breezejs.com/sites/all/apidocs/classes/EntityType.html
Object.keys(data.result[0]) is the vanilla JavaScript way to get all properties of the data.result[0] object. Just saying.
Jay's way of course winnows those down to the properties monitored by Breeze, the persisted properties in particular. That's probably what you meant :-)
For my application I only need to determine the namespace of the root node, so ideally I would like to execute a single operation to get this namespace.
I have code that uses an XPath to get all of the namespaces declared (/*/namespace::*), but then I have to go through them all and do vn.toRawString for each one to compare it to my prefix, which I would like to avoid.
I'm sure I'm missing something obvious here, but what is the best way to get the namespace of a given element, since xmlns: attributes are not considered in getAttrValNS ?
String elementName = vn.toRawString(vn.getCurrentIndex());
String prefix = prefix(elementName);
String localName = localName(elementName);
QName rootQName;
if (prefix == null) {
rootQName = new QName(localName);
} else {
int nsIndex = vn.getAttrValNS("xmlns", prefix);
// Can't find index because xmlns attributes are not included
String namespace = vn.toRawString(nsIndex);
rootQName = new QName(namespace, localName);
}
Is there a simple XPath that will just give me the namespace of the root node, not ALL namespaces DECLARED on the root node?
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("namespace-uri(.)");
String ns = ap.evalXPathToString();
I'm implementing a custom data provider, I have gotten it to the point that it returns data and can be filtered, but am having some trouble getting relationships to work.
When querying the metadata the relationships look correct, and when querying a table the related property links appear, but when attempting to access a ResourceReference property I get the following exception:
Object reference not set to an instance of an object.
System.NullReferenceException
stacktrace at System.Data.Services.Providers.DataServiceProviderWrapper.GetResourceAssociationSet(ResourceSetWrapper resourceSet, ResourceType resourceType, ResourceProperty resourceProperty)
at System.Data.Services.Providers.DataServiceProviderWrapper.GetContainer(ResourceSetWrapper sourceContainer, ResourceType sourceResourceType, ResourceProperty navigationProperty)
at System.Data.Services.Providers.DataServiceProviderWrapper.GetResourceProperties(ResourceSetWrapper resourceSet, ResourceType resourceType)
at System.Data.Services.Serializers.SyndicationSerializer.WriteObjectProperties(IExpandedResult expanded, Object customObject, ResourceType resourceType, Uri absoluteUri, String relativeUri, SyndicationItem item, DictionaryContent content, EpmSourcePathSegment currentSourceRoot)
at System.Data.Services.Serializers.SyndicationSerializer.WriteEntryElement(IExpandedResult expanded, Object element, ResourceType expectedType, Uri absoluteUri, String relativeUri, SyndicationItem target)
at System.Data.Services.Serializers.SyndicationSerializer.WriteTopLevelElement(IExpandedResult expanded, Object element)
at System.Data.Services.Serializers.Serializer.WriteRequest(IEnumerator queryResults, Boolean hasMoved)
at System.Data.Services.ResponseBodyWriter.Write(Stream stream)
Here's a sample of how I create the relationships:
var sourceReference = new ResourceProperty(
relatedType.ResourceTypeName,
ResourcePropertyKind.ResourceReference,
relatedType.ResourceType);
sourceReference.CanReflectOnInstanceTypeProperty = false;
compoundType.ResourceType.AddProperty(sourceReference);
var destinationReference = new ResourceProperty(
compoundType.ResourceSetName,
ResourcePropertyKind.ResourceSetReference,
compoundType.ResourceType);
destinationReference.CanReflectOnInstanceTypeProperty = false;
source.ResourceType.AddProperty(destinationReference);
var sourceAssociation = new ResourceAssociationSet(
"source",
new ResourceAssociationSetEnd(compoundType.ResourceSet, compoundType.ResourceType, sourceReference),
new ResourceAssociationSetEnd(relatedType.ResourceSet, relatedType.ResourceType, null));
var destinationAssociation = new ResourceAssociationSet(
"destination",
new ResourceAssociationSetEnd(relatedType.ResourceSet, relatedType.ResourceType, destinationReference),
new ResourceAssociationSetEnd(compoundType.ResourceSet, compoundType.ResourceType, null));
From looking at the sample code on the OData website I thought I'd done it all correctly, and cannot determine my error. Any ideas? or tips on debugging a custom WCF Data service?
Update:
Here's what happens just before the null exception.
Have a resource set Collars with a relationship to Projects so I do this query:
blah.svc/Collars(1)/Project
My override of GetResourceAssociationSet in my IDataServiceMetadataProvider gets called with the parameters ResourceSet = Collars, ResourceType = Collar, Property = Project and I return the association set specified above.
GetResourceAssociationSet is then called again with ResourceSet = Projects, ResourceType = Collar, Property = Project and I return the same association set.
Then in System.Data.Services.Providers.GetResourceAssociationSetEnd the variables passed in are resourceSet = Projects, resourceType = Collar, resourceProperty = Project, this function returns null.
Which makes thisEnd in System.Data.Services.Providers.DataServiceProviderWrapper.GetResourceAssociationSet equal to null.
Then GetRelatedResourceAssociationSetEnd is called with the same variables and also returns null.
So it then crashes with the call
ResourceSetWrapper relatedSet = this.ValidateResourceSet(relatedEnd.ResourceSet);
because relatedEnd is null.
Well, in my debugging I noticed the last time GetResourceAssociationSet was called before the error occurred was for a case where the resourceSet and resourceType parameters had different values (in the case of a derived type).
So, I looked and found this
WCF data services (OData), query with inheritance limitation?
...and lo and behold this uninformative null reference exception (at least in my case) is caused by that same issue. I removed the offending property (and then moved it to the base resource set, even though it doesn't exist there in practice), and the issue was resolved.
Interesting side note (in case this helps the original poster): ResourceType.Properties includes properties from Base Types. Had to change some code to use PropertiesDeclaredOnThisType instead...
The solution for me was that I had made a mistake in my implementation of GetResourceType in my IDataServiceQueryProvider.
When querying a Resource Property that was a ResourceSetReference I was returning the ResourceType of the parent resource, and not the type of the related resource.