XML file to database table + model class generator - java

I have a requirement which is as follows. Lets say I have a XML file which contains the following content.
<?xml version="1.0"?>
<items>
<item id="1" class="Company" interface="true">
</item>
<item id="2" class="MyCompany" implements="Company">
<attribute name="name" value="CompanyB"/>
<attribute name="vatNumber" value="5678"/>
<reference name="CEO" ref_id="11"/>
<reference name="address" ref_id="19"/>
<collection name="contractors">
<reference ref_id="4"/>
<reference ref_id="3"/>
</collection>
<collection name="departments">
<reference ref_id="7"/>
<reference ref_id="6"/>
</collection>
<collection name="oldContracts">
<reference ref_id="4"/>
<reference ref_id="3"/>
</collection>
<collection name="secretarys">
<reference ref_id="14"/>
<reference ref_id="15"/>
</collection>
</item>
</items>
I want to have a convertor to convert this items file into database tables directly and on the same time generate model classes for each type of items. So than I can have a modelservice to control them .Also when i add more attributes to the type it should be able to update the database table and the model code automatically.
In more better way I require some kind of model code generator which create database tables and maps them both in a well planned manner ?
Is some kind of tool already avaiable to do this type of buisness logic.??? I don't like to reinvent the wheel again.?

I'd suggest you you to develop code that transforms your XML to Hibernate HBM file. Then use Hibernate as a persistence layer.
You can even probably create XSLT transformation from your format to HBM.

Actually after little googling I came across Apache Torque which looks awesome from my needs.

Related

How to pass customized parameters to SOlR DIH query

I have scenario where i need to pass customized parameters to solr data import query.
Ex- select * from customer where last_updated_date >=last_updated_indexed_date
The last_updated_indexed_date is coming from another table which has details about core.
How can I pass that last_indexed_updated_date in DIH query.
The data-config can be configured something like below :
<dataConfig>
<dataSource name="ds-db" driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:#127.0.0.1:1521:test" user="dev" password="dev" />
<dataSource name="ds-file" type="BinFileDataSource" />
<document name="documents">
<entity name="book" dataSource="ds-db"
query="select distinct
book.id as id,
book.title,
book.author,
book.publisher,
from Books book
where book.book_added_date >= to_date($ {dataimporter.request.lastIndexDate}, 'DD/MM/YYYY HH24:MI:SS')))"
transformer="DateFormatTransformer">
<field column=”id” name=”id” />
<field column=”title” name=”title” />
<field column=”author” name=”author” />
<field column=”publisher” name=”publisher” />
<entity name=”content” query=”select description from content
where content_id='${book.Id}' ”>
<field column=”description” name=”description” />
</entity>
</entity>
</document>
</dataConfig>
The way here '${book.Id}' is retrieved and passed to another query. You will also need to work upon something similar for the last_indexed_updated_date in your data-config.xml. if you don't have the same in your tables. You can try the same passing to the data import url like lastIndexDate(Please refer the below data import url.)
The data import url will be be like
http://localhost:8080/solr/admin/select/?qt=/dataimport&command=full-import&clean=false&commit=true&lastIndexDate='08/05/2011 20:16:11'

How to add Annotations elements in metadata generated by Apache Olingo V2.0?

I have developed Odata service for a system entity which generates a metadata but however I cant figure out how to add Annotations element to it. Sample Metadata generated is as follows :-
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:sap="http://www.sap.com/Protocols/SAPData" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
m:DataServiceVersion="1.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="myNamespace" sap:schema-version="1">
<EntityType Name="System">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="name" Type="Edm.String" sap:label="System Name" sap:creatable="false"
sap:updatable="false" sap:sortable="false" sap:required-in-filter="true"/>
<Property Name="description" Type="Edm.String" />
<Property Name="status" Type="Edm.String" />
<Property Name="type" Type="Edm.String" />
</EntityType>
<EntityContainer Name="ODataEntityContainer" m:IsDefaultEntityContainer="true">
<EntitySet Name="Systems" EntityType="myNamespace.System" />
<FunctionImport Name="NumberOfSystems" ReturnType="Collection(myNamespace.System)"
m:HttpMethod="GET" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
I need to add following elements to above metatada
<Annotations Target="myNamespace.System"
xmlns="http://docs.oasis-open.org/odata/ns/edm">
<Annotation Term="com.sap.vocabularies.UI.v1.LineItem">
<Collection>
<Record Type="com.sap.vocabularies.UI.v1.DataField">
<PropertyValue Property="Value" Path="name" />
</Record>
<Record Type="com.sap.vocabularies.UI.v1.DataField">
<PropertyValue Property="Value" Path="description"/>
</Record>
<Record Type="com.sap.vocabularies.UI.v1.DataField">
<PropertyValue Property="Value" Path="status" />
</Record>
</Collection>
</Annotation>
</Annotations>
I came across the org.apache.olingo.commons.api.edm.provider.annotation package but cant find any suitable API. Please let me know how should I proceed.
Thanks in advance.
The annotations you would like to use have been introduced with OData V3 which is why they are not directly supported with the Olingo V2 library.
You can use the EdmProvider AnnotationElement and AnnotationAttribute classes to mimic this behaviour though. For example You can create a AnnotationElement with the name "Annotations" this element will then have the "AnnotationAttribute" Target=SomeString. Since an "AnnotationElement" can have child elements you can put your Collection element there. Namespaces are also handled with "AnnotationAttributes".
You can only attach the annotation to Edm elements which are derived from the EdmAnnotatable interface. So this is a difference to V3.
This is currently the only way to get this behaviour with Olingo V2.

Generate source files from XML

I have the XML file which starts like this:
<?xml version="1.0" encoding="UTF-8"?>
<interface name="AccountAPING" owner="BDP" version="1.0.0" date="now()" namespace="com.betfair.account.api"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<description>Account API-NG</description>
...
afterward there are various blocks, such as:
<operation name="getDeveloperAppKeys" since="1.0.0">
<description>
Get all application keys owned by the given developer/vendor
</description>
<parameters>
<request/>
<simpleResponse type="list(DeveloperApp)">
<description>
A list of application keys owned by the given developer/vendor
</description>
</simpleResponse>
<exceptions>
<exception type="AccountAPINGException">
<description>Generic exception that is thrown if this operation fails for any reason.</description>
</exception>
</exceptions>
</parameters>
</operation>
........
<simpleType name="Status" type="string">
<validValues>
<value name="SUCCESS">
<description>Sucess status</description>
</value>
</validValues>
</simpleType>
........
<dataType name="TimeRange">
<description>TimeRange</description>
<parameter name="from" type="dateTime" mandatory="false">
<description>from, format: ISO 8601)</description>
</parameter>
<parameter name="to" type="dateTime" mandatory="false">
<description>to, format: ISO 8601</description>
</parameter>
</dataType>
How can I generate Java code from this using maven? I tried using "maven-jaxb2-plugin", but it can't parse this structure.
Please note
This is an XML file not not an xsd
I'm using Netbeans
First of all, you need the schema (xsd) that describes your xml sample. Without that schema you can not use Jaxb. You don't have a schema for the sample you shown xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" is not the schema for your xml.
You can use free on-line tools to generate schemas from xml, but you can't rely on this tools without review the automated schema.
To generate Java code from a schema file use XJC, see here. Open a command prompt to the folder where you put your xsd file, and then generate java code you'll just need to type:
$ xjc nameOfSchemaFile.xsd
xjc is included with Java SDK.

MyBatis collection in association only return one row

I've to use MyBatis for a project and I'm trying to define a resultMap with a collection in my association.
I'll simplify my problem, so we suppose that my database have two table :
World
[idWorld, worldName]
Level
[idLevel,
levelName,
idWorld]
I want to get each world and all levels associated.
At the moment, I manage to get all result with this result map :
<resultMap type="World" id="WorldResultMap">
<id property="idWorld" column="idWorld"/>
<result property = "worldName" column="worldName"/>
<collection property="level" javaType="java.util.ArrayList" ofType="Level">
<result property="idLevel" column="idLevel" />
<result property="levelName" column="levelName" />
</collection>
</resultMap>
But after mapping I get a result like that :
<worlds>
<world>
<idWorld>1</idWorld>
<worldName> Wordl's name</worldName>
<level> ... </level>
<level> ... </level>
</world>
<world>
<idWorld>2</idWorld>
<worldName> Wordl's name</worldName>
<level> ... </level>
<level> ... </level>
<level> ... </level>
</world>
<worlds>
When I want something like that :
<worlds>
<world>
<idWorld>1</idWorld>
<worldName> Wordl's name</worldName>
<levels>
<level> ... </level>
<level> ... </level>
</levels>
</world>
<world>
<idWorld>2</idWorld>
<worldName> Wordl's name</worldName>
<levels>
<level> ... </level>
<level> ... </level>
<level> ... </level>
</levels>
</world>
<worlds>
I'm trying to get this result with the following result map :
<resultMap type="World" id="WorldResultMap">
<id property="idWorld" column="idWorld"/>
<result property = "worldName" column="worldName"/>
<association property="levels" javaType="Levels">
<collection property="level" javaType="java.util.ArrayList" ofType="Level">
<result property="idLevel" column="idLevel" />
<result property="levelName" column="levelName" />
</collection>
</association>
</resultMap>
I change my entity in order to have the list of level into levels and not anymore into world.
My problem is I get only one level for each world in my last result map with the same query.
I think I miss understand the use of "association".
How can I get all my level into levels ?
Any help will be appreciated, thanks in advance.
replace
<result property="idLevel" column="idLevel" />
to
<id property="idLevel" column="idLevel" />
then you are supposed to get the expected result.
Quote from the mybatis-doc#Result_Maps:
Very Important: id elements play a very important role in Nested Result mapping. You should always specify one or more properties that can be used to uniquely identify the results. The truth is that MyBatis will still work if you leave it out, but at a severe performance cost. Choose as few properties as possible that can uniquely identify the result. The primary key is an obvious choice (even if composite).
If id is absent, "MyBatis will still work". It seems that MyBatis will filter out duplicated rows it thinks.

Mapping collection to XML in Castor

I'm trying to map a POJO to XML using Castor.
Let's say I have a Order that has a collection of Items... is there any way of achieving an xml like the following:
<order>
...order attributes
<items>
<item> ..item attributes </item>
<item> ..other item </item>
</items>
</order>
I could make something similar but without the <items> node. This wouldn't be a problem in other case but my XML must adhere to a strict XSD schema so I need to do it like that.
Thanks!
I though of a kind of "workaround" that would involve creating a new java object (that would be the node) that would only contain the list of items... can anyone think of a better approach? there's a 100 rep bounty open since now!
You can use the location attribute of the bind-xml lement
http://castor.codehaus.org/1.2/xml-mapping.html#6.-Location-attribute
Example from the docs:
<class name="Foo">
<field name="bar" type="Bar">
<bind-xml name="bar" location="abc"/>
</field>
</class>
Produces the following XML:
<foo>;
<abc>
<bar>...</bar>
</abc>
</foo>
The other answer doesn't use the collection attribute which I think is probably what you're ultimately needing.
Something like this might work when included in your mapping for the Order object:
<field name="items" type="item" collection="arraylist" >
<bind-xml name="items" node="element"/>
</field>

Categories