MapStruct: Issue with Nested Properties and ReportingPolicy.ERROR on Unmapped Source Properties - mapstruct

Using MapStruct, we want to use ReportingPolicy.ERROR, and have code like the following:
#Mapping(source = "nestedSource.doublyNestedSourceField", target = "nestedTarget.doublyNestedTargetField")
Target mapSourceToTarget(Source source);
Where nestedSource is not the same type as nestedTarget, and both doublyNested*Field types are String.
There is no mapper declared for NestedSource -> NestedTarget. The String properties declared in the Mapping above are the only ones in those types.
The above causes an unmapped source error:
Unmapped source property: "doublyNestedSourceField".
That seems more-or-less reasonable, as we didn't declare a mapper for NestedSource -> NestedTarget.
However, here's the issue: If we change the ReportingPolicy for unmapped sources to warn/ignore, MapStruct figures out how to correctly map the doublyNestedSourceField in the mapper implementation, even though it claims there is no source mapping present. Just wondering what is going on here, and whether I'm missing something.
----Into the weeds a bit more (in the MapStruct code itself)----
I could be doing something wrong, but I did notice that in BeanMethodMapping.java MapStruct attempts to remove "nestedSource.doubleNestedSourceField" from unprocessedSourceProperties, even though the key for the appropriate property is just "nestedSource" in unprocessedSourceProperties. Thus "nestedSource" is left as an unprocessed source property and an error is thrown.

Related

How does Jackrabbit generate jcr:uuid (in AEM)?

I am trying to create an auto-generated GUID property on all cq:PageContent nodes. This will be similar to the jcr:uuid property, but will be persisted with content promotion/replication/package installs (whereas the jcr:uuid for a content item changes between different environments).
I am trying to determine how AEM/JCR generates the jcr:uuid property on node creation. The CND defining the property is:
[mix:referenceable]
mixin
- jcr:uuid (string) mandatory autocreated protected initialize
I've tried defining my GUID property in a similar manor, specifying the autocreated and initialize attributes, but this did not result in auto-generation of the property.
Could anybody point me to the source of the jcr:uuid's generation?
As an aside, I asked a related question on the Adobe Community Forum: http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.5_ciot.html/forum__bnxr-i_am_tryingtocreat.html
You don't mention which version of AEM (so whether you're dealing with Jackrabbit or Oak), but the mechanism turns out to be basically the same.
When assigning a default value, there are a few hard-coded system property names that get special treatment (jcr:uuid being one of them). If the name of the property being assigned a default value doesn't match any of the special cases, it falls back the static list of default values from the property definition (e.g. listed in the CND file).
In summary, it looks like you cannot piggy-back on this mechanism to assign your own dynamic default value for an arbitrary property. You would need to implement your own event listener or something.
Jackrabbit: See the implementation of setDefaultValues and computeSystemGeneratedPropertyValues
Oak: See the implementation of TreeUtil autoCreateProperty

MEF: metadata seem to override interface when using GetExports

I'm building a MEF-based plugin-centric WPF application and I'm facing an issue with GetExports, maybe it's just my ignorance but I find an odd behaviour. I have a number of exported parts, all derived from 2 different interfaces (let's name them A and B), but all marked with the same metadata attribute X. So I have code like:
[Export(typeof(A))]
[TheXAttributeHere...]
public class SomePart1 : A { ... }
for each part, and the same for classes implementing B:
[Export(typeof(B))]
[TheXAttributeHere...]
public class SomePart2 : B { ... }
Now, when I try getting all the parts implementing A and decorated by attribute X with some values, MEF returns not only the A-implementing parts, but ALSO the B-implementing parts. So, when I expect to deal with A-objects I get a B, whence a cast exception.
In the real world, interfaces are named IItemPartEditorViewModel and IItemPartEditorView, while their common attribute is named ItemPartEditorAttribute and exposes a PartType string property on which I do some filtering. My code to get parts is thus like e.g.:
var p = (from l in container.GetExports<IItemPartEditorViewModel, IItemPartEditorMetadata>()
where l.Metadata.PartType == sPartType
select l).FirstOrDefault();
When looking for IItemPartEditorViewModel whose PartType is equal to some value, I get the IItemPartEditorView instead of IItemPartEditorViewModel implementing object. If I comment out the attribute in the IItemPartEditorView object instead, I correctly get the IItemPartEditorViewModel implementing object.
Update the suggested "templated" method was used, but I mistyped it here as I forgot to change lessthan and greaterthan into entities. Anyway, reviewing the code I noticed that in the attribute I had "ViewModel" instead or "View" for the interface type, so this was the problem. Shame on me, sorry for bothering :)!
I think I'd need to see more of the code to know for sure what's going on. However, I'd suggest you call GetExports like this:
// Get exports of type A
container.GetExports<A>();
// Get exports of type B
container.GetExports<B>();
Then do your filtering on the list returned. This will probably fix the cast issues you are having. I'd also be interested in seeing the code for the custom metadata attribute. If it derives from ExportAttribute for example, that might be part of the problem.

What is the "Func<object> modelAccessor" parameter for in MVC's DataAnnotationsModelMetadataProvider?

It's one of the parameters supplied to the CreateMetadata method (which you override if extending metadata support).
ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes,
Type containerType,
Func<object> modelAccessor, <<--THIS ONE
Type modelType,
string propertyName)
I had assumed that it allowed you to access the model object itself (e.g. for setting metadata based on model values), however when I try to use it to cast to my model object I just get null.
Entity ent = (Entity)modelAccessor(); // = Null
If I've missunderstood, can anyone explain what it's purpose is? Or alternatively, how to properly use it?
Thanks
We originally had that as "object model", rather than "Func modelAccessor". We had to change it late in MVC 2's ship cycle.
The purpose is to delay retrieving the actual value of the model until such point as you know you're going to need it (that is, until you call ModelMetadata.Model).
The problem it solves is actually a rather esoteric one related to model binding against a LINQ to SQL class that has a foreign key reference in it. The problem is, if you've retrieved the child object which is represented by a foreign key relationship (which usually means a delay load of that object), then you're no longer allowed to choose a new child object by setting the foreign key ID property. It's very common to model bind the foreign key ID (and not the whole foreign key entity) when model binding, but if we'd retrieved the foreign key entity object (for the purposes of populating the ModelMetadata class) then that binding would no longer be legal, and actually throw an exception. Since ModelMetadata is used for both directions of models -- inbound, via model binding, and outbound, via HTML generation -- we needed to introduce the layer of indirection to protect your ability to use it in both scenarios without disrupting LINQ to SQL's rules.
The modelAccessor parameter does not point to an instance of the object, but rather it is a function that will access some attribute of your object. The Func "encapsulates a method that has no parameters and returns a value of the type specified by the TResult parameter." For example, if we have following class:
public class Bar(){
[DisplayName("I am Foo.")]
public string Foo{get;}
}
When the CreateMetaData is called, it will be to create meta data for the Foo property and the modelAccessor will be a function that returns the value of Foo.
I did a little digging and found a way to get to the instance of the object, but it requires using reflection. You can do the following to get the Bar class in my example:
if (modelAccessor != null)
{
//Use reflection to get the private field that holds the Bar object.
FieldInfo container = modelAccessor.Target.GetType().GetField("container");
//Invoke field on the modelAccessor target to get the instance of the Bar object.
Bar myObject = (Bar)container.GetValue(modelAccessor.Target);
}
I've only run this against a simple test case, so your mileage may vary, but hopefully this will help clarify what is going on.

EntityFramework how to Override properties

I've just started using EF in VS2010. That thing is just amazin'.
I frankly can't understand something. For example I have EntityType with property, they generated from database structure.
Now, I have to simply override that property in my code. I don't need to save value of the property back into DB, but everytime when it gets read from DB it should be substituted with run-time calculated value.
Of course I can create derived class based on my EntityType but I've tried and found kinda difficulties, I'm not sure this is kinda right way to do. Anyway even when I try to change the whole EntityType to Abstract, damn Visual Studio doesn't want to validate that and says something like:
"Error 2078: The EntityType 'AssetsModel.Asset' is Abstract and can be mapped only using IsTypeOf."
"Error 2063: At least one property must be mapped in the set mapping for 'Assets'"
What the hell is this suppose to mean I dunno..
Any ideas?
The best approach would be to use Partial Classes and then create a new ReadOnly property to calculate the value in the getter.

Entity Framework 4.0: Create an unmapped property in the model (currenty i get : error 11009 - Property is not mapped)?

I know that i can enter/add new properties via code manually into partial classes but i wanted to use the model to add my new properties - reason being is that i can control a number of different attributes like NULL and things like that... and of course the code generations works great..
I added some foreign keys manually just on the model and they work great.
But everytime i add a SCALER PROPERTY i get an error in vs 2010 which says
Error 2538 Error 11009: Property 'testprop' is not mapped.
I can't believe i must map a custom property that i created to a column in the db.... is there no way to say "IGNORE" this property or treat as an unmapped property??
This way my code generation will create the required items BUT i don't get the error
Any help on this would be really helpful.
As i say i know i can edit things manually but wanted to update the model rather than edit a partial class....
I am sure i am missing something obvious?
With EntityFramework 5 you can use the NotMappedAttribute for unmapped properties. So, you can migrate to EF5 or use partial classes on EF4.
I believe that EF will on allow you to use the Model Designer to map to something that exists. If you want to create a property that doesnt exist, you'll have to use the partial class.
I had the same error - you can use the NotMappedAttribute for unmapped properties...