Converting XSD to ECore - eclipse

I am currently trying to convert an XML Schema to ECore with the Eclipse Modeling Framework. EMF offers the attributre ecore:reference to indicate that an attribute should be translated into an EReference. However, this only works if the attribute is of type IDREF or anyURI. My problem is that any ID referenced via IDREF has to be unique througout the whole XML document. However, my XML has the following structure:
<A id="a1">
<B id="b1">
<ref idref="b2" />
</B>
<B id="b2">
<ref idref="b1" />
</B>
</A>
<A id="a2">
<B id="b1">
<ref idref="b2" />
</B>
<B id="b2">
<ref idref="b1" />
</B>
</A>
The ids of the B-elelemts are only unique inside the sourrounding A-element. Thus the parser will throw an error if i declare the ids of the B-elements as type ID.
How can I tell EMF that idref is a reference to the attribute id of B-element which belongs to the same A-element?

If the XML Schema is really using ID and IDREF, then the instance you show isn't valid with respect to that schema. Isn't that a problem? It's more like a key/keyref, which isn't supported by Ecore either.
If it just comes down to wanting an Ecore model that can read and write such instances I would define an attribute idref that's just a string and define a transient reference resolvedRef of type A and I'd modify the getters and setters so that each derives sensibly from the other. I.e., when you call getResolvedRef, it would check if the field for that is null, and if the field for the idref has a value, it would walk the model to resolve (look up that name in the appropriate scope) and store it in the field.
It's a bit tricky to define the mutual derivation in a sensible way for both getters and both setters, but it should be possible.

Related

codefluent custom stored procedure

I have a custom stored procedure with in parameters that return fields of different tables how I can map this custom stored to an entity? I only want to use like a read only values for a report I don't want to save or something like that I try to add the extra fields to the most similar entity but when I execute the method in code the extra fields are null
Solution 1: Using a view
A view allows to aggregate data from different entities.
<Article>
<Id />
<Name />
<Lines typeName="LineCollection" />
<cf:method name="LoadArticlesByCommand" body="load(string commandName) from ArticleByCommand where CommandName = #commandName" />
<cf:view name="ArticleByCommand" autoLightweight="true">
<ArticleName expression="Name"/>
<ArticleQty expression="Lines.Quantity" />
<CommandName expression="Lines.Command.Name" />
</cf:view>
</Article>
<Command>
<Id />
<Name />
<Lines typeName="LineCollection" />
</Command>
<Line setType="List">
<Article typeName="Article" key="true" />
<Command typeName="Command" key="true" />
<Quantity typeName="int" />
</Line>
http://blog.codefluententities.com/2014/04/22/views-auto-lightweight-and-the-modeler/
https://www.softfluent.com/documentation/Views_PersistentViews.html
Solution 2: Using a lightweight entity
Instead of creating a view, you can can create a lightweight entity that contains only the properties used by the stored procedure.
<cf:entity name="Person" lightweight="true">
<cf:property name="FirstName" typeName="string" />
<cf:property name="lastName" typeName="string" />
<cf:method name="ComputeBalance"
body="load () raw"
rawBody="SELECT 'John' AS FirstName, 'Doe' AS LastName" />
</cf:entity>
Solution 3: Custom mapping
For more specific values or types, a custom method can be provided to map the database values to .NET types. This custom method will be called with a DataReader as parameter, meaning that a developer could do whatever he wants.
<cf:entity name="Sample">
<cf:method name="LoadPair" body="raw" rawBody="SELECT 1234,5678"
returnTypeName="CodeFluent.Runtime.Utilities.Pair<System.Int32,System.Int32>"
cfom:methodName="On{0}" />
<cf:snippet>
private static CodeFluent.Runtime.Utilities.Pair<int,int> OnLoadPair(System.Data.IDataReader reader)
{
return new Pair<int, int>(reader.GetInt32(0), reader.GetInt32(1));
}
</cf:snippet>
</cf:entity>
You can also use OnAfterReadRecord or OnBeforeReadRecord rules
If it is not essential that you map the results of the custom stored procedure to an entity than another option is to use the built in support for DataSets.
http://blog.codefluententities.com/2011/06/22/dataset-support-in-codefluent-entities/
<cf:method name="LoadAllCities" body="raw" returnTypeName="System.Data.DataSet">
SELECT $Address::City$ FROM $Address$
</cf:method>
.
DataSet ds = Address.LoadAllCities();
foreach (DataTable table in ds.Tables)
{
foreach (DataRow row in table.Rows)
{
Console.WriteLine("City: " + row[0]);
}
}
Upon re-reading you're question I am providing another answer.
In response to the part where you said "I try to add the extra fields to the most similar entity but when I execute the method in code the extra fields are null". The following steps should be able to solve that problem.
Execute one of the automatically created stored procedure in SQL Management Studio.
Execute the stored procedure you manually created.
Verify that the fieldnames returned by both stored procedures match.
I think the above will solve your immediate problem but I still don't like the solution. The reason is that you said you picked the most similar entity. I think that is going to cause problems in the future especially if the stored procedure is not being mapped to all of the entities properties.
I would recommend either lightweight entity, view or DataSet.

Alfresco Custom Data List Type Text Field Help Text

I've successfully created a new Data List type in Alfresco, but was struggling to figure out what the tag is called to achieve a description under a field. I consulted the Data Dictionary, but was unable to find it.
It may not exist, but the this screenshot makes me hopeful. As you can see, the Modified Date field has the DD/MM/YYYY help text under it. This is exactly what I want to put under the Program Name field, but with my own string.
<property name="orpdl:programName">
<title>Program Name</title>
<type>d:text</type>
<helptext>What's this tag called?</helptext>
<mandatory>true</mandatory>
</property>
Eeuh that's just a div in the control field for all the date fields.
Take a look at alfresco/site-webscripts/org/alfresco/components/form/controls/date.ftl
The message you see is in this div:
<div class="format-info">
<span class="date-format">${msg("form.control.date-picker.display.date.format")}</span>
<#if showTime><span class="time-format<#if disabled>-disabled</#if>">${msg("form.control.date-picker.display.time.format")}</span></#if>
</div>
So you want a string, just create your own custom controller based on textfield.ftl and put whatever string you like. Probably best to do that by passing it through the control-params.

Can I get ServiceStack to serialize specific properties as XML attributes instead of elements?

My ServiceStack API is returning the following XML:
<UserResult>
<Email>dan-dare#the-eagle.com</Email>
<Forenames>Daniel</Forenames>
<Href>/users/00000001-0000-0000-0000-00000000000d</Href>
<Memberships>
<Href>/users/00000001-0000-0000-0000-00000000000d/memberships</Href>
</Memberships>
<Surname>Dare</Surname>
<UserGuid>00000001-0000-0000-0000-00000000000d</UserGuid>
<Username>test_dan</Username>
</UserResult>
I'm curious as to whether I can modify the XML serialization so it returns the HREF properties as attributes rather than elements:
<UserResult href="/users/00000001-0000-0000-0000-00000000000d">
...
<Memberships href="/users/00000001-0000-0000-0000-00000000000d/memberships" />
...
</UserResult>
or even override the default serialization entirely so that particular properties are exposed as <link /> elements:
<UserResult href="/users/00000001-0000-0000-0000-00000000000d">
...
<link rel="memberships" href="/users/00000001-0000-0000-0000-00000000000d/memberships" />
...
</UserResult>
I'm undecided as to whether I should do this, but very curious to know whether it's possible - either by decorating the properties or by replacing the default XML serializer (DataContractSerializer?) used by ServiceStack.

Spring MVC 3 - custom labels in <form:select>

I'm creating a form in which user will be able to choose (among the others) the factory of a product.
Each factory is identified by and ID and has specific address.
I want to use custom label in following code:
<form:select items="${factories}" path="factory" itemValue="id" itemLabel="..."/>
At first i tried using Spring Formatter functionality (org.springframework.format.Formatter interface), but when I did this, and when I removed "itemLabel" attribute to have it displayed automatically via Formatter):
<form:select items="${factories}" path="factory" itemValue="id"/>
But then It wasn't selecting proper value if it was set (in case of editing).
Then I tried to:
<form:select path="factory" itemValue="id">
<c:forEach ...>
<form:option value="${factory.id}" label="${factory.address.city} ${factory.address.street}"
</c:foreach>
</form:select>
But as in earlier solution spring was not selecting proper value that was set in model.
My question is:
Is it possible to format entity in a way, that form:select works properly, when a value of select field is not the same as its label.
I had the same problem, the trouble is the form:option value and label maps directly to a property rather than having the flexibility to specify jstl.
So you have to hand crank the html, to something like:
<select name="factory">
<c:forEach var="factory" items="${factories}" >
<option value="${factory.id}" label="${factory.address.city} ${factory.address.street}"/>
</c:forEach>
</select>
Spring will pick up the 'path' based on the name attribute('factory' in this case), as it will try and map to the model object you are using automatically.
Another way is to add another field to the model to concatenate and format label as you wish. And if its an #Entity object then make the field #Transient.
You can also override toString() method in Factory class
#Override
public String toString() {
return "desired string";
}
Then in your jsp
<form:select items="${factories}" itemValue="id" path="factory"/>

Spring #ModelAttribute doesn't care about commandName

JSP:
<form:form commandName="editWeather" method="post" action="../edit">
<!-- Input fields -->
<input type="submit" value="Submit">
</form:form>
And this is how I get the model in Spring:
#ModelAttribute("DONTGIVEADAMN") Weather weather
And I can still use the weather to do my operations and it works great, for example:
weatherService.editWeather(weather);
My question is...Why does this work?
Model attribute name doesn't matter when binding data received from a form (because names of form fields correspond to the names of fields of the model object), it matters only when rendering a form.
I particular, when model attribute name in your POST handler method doesn't match the commandName in the form, you will be able to receive the data, but won't be able to redisplay a form with validation errors.
its matching the class type (or interface), not the name of the variable/parameter; and the specified request mapping/method signature must be correct.