Thursday, July 12, 2012

Import files on Oracle MDS from Weblogic EM Console

In this post, I would demonstrate the method to upload your MDS (Metadata services) artifacts like wsdls, schemas, xsls etc onto the Weblogic server through the EM console.

The steps
 1. Create a zip called apps.zip having the hierarchy of folders and files as you want them on your local drive. So in the illustration below I wish to upload the xml schema Sample_Schema.xsd to the mds. Therefore I created the following hierarcy, the way I want it inside the root folder apps.


  
 2. Login to em console using http://<hostname>:<portname>/em and navigate to soa_infra > Administration > MDS Configuration



3. Import the zip onto the server by clicking the import button as shown below -



4. You will receive a success pop-up once the import was successful. 
You can now refer the mds file Sample_Schema.xsd in composite/wsdl etc via the url oramds:/apps/ProjectSchemas/SCP/Inbound/Sample_Schema.xsd


5. Likewise, you could export your mds files as well using the export button above.

Tuesday, June 12, 2012

Oracle 11g DB Adapter Unique Constraint/Sequence mismatch

In my last Project, I was using a DB adapter in my BPEL code. It was using an Oracle  seq 'DEMO_INSERT_S' as below -

DB Adapter with Sequence


















Problem -


Even though the sequence was an Oracle generated sequence, I faced a unique issue in PROD. The sequence was incrementing of its own accord with no relation whatsoever with the actual seq.NEXTVAL. So while the DB adapter was generating a seq value say 101, it so happened that 101 already existed in the database resulting in a UNIQUE CONSTRAINT error! It took us some time to figure out the issue to be as follows -
Oracle BPEL DB Adapter follows or rather maintains a sequence of its own and does not exactly use the oracle generated seq.NEXTVAL.
Workaround -


So instead of providing the SEQUENCE in the DB adapter and let it have a mind of its own, we removed it from there and provided the Oracle sequence generated NEXTVAL in the xsl transformation.
So here's the mapping for the primary key of the DEMO_INSERT_TABLE. We are no longer dependent on the DB adapter for the sequence, instead we use the bpel extension function oraext:sequence-next-val to derive it from the actual Oracle Sequence i.e. DEMO_INSERT_S and pass it to the DB adapter mapping xsl.


<ns1:DemoInsertTable>

     <xsl:for-each select="/ns0:ChangeOrderCanonical/ns0:Parts">
        <ns1:GglPlmItemUpload>
          <!--ns1:id>
            <xsl:value-of select="oraext:sequence-next-val('APPS.DEMO_INSERT_S','jdbc/MyDataSource')"/>
          </ns1:id-->
 <!-- other column mappings -->
</ns1:DemoInsertTable>

This resolved my issue and hope it helps you too! 










Thursday, May 17, 2012

Validate XML against XML Schema using Java

Taking up the Employees.xsd schema and Employees.xml again for this illustration as well.
We want to validate the xml instance against the schema and want to achieve this through Java code. Not challenging, eh? Ageed, however thought I'd pen it here for future references.

Here's the code that achieves it -
public class SchemaValidation {

  private static void validateXML(File sourceFile, File schemaFile) throws IOException,SAXException {

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = schemaFactory.newSchema(schemaFile);

    Validator validator = schema.newValidator();
    try {
      validator.validate(new StreamSource(sourceFile));
      System.out.println("is valid");
    } catch (SAXException e) {
      System.out.println("not valid because " + e.getLocalizedMessage());
    }
  }

  public static void main(String args[]) throws ParserConfigurationException, IOException,
      SAXException {

    File file = new File("Employees.xml");
    File schemaFile = new File("Employees.xsd");
    SchemaValidation.validateXML(file, schemaFile);
  }
}
So when I try validating the XML below against the Employees xsd schema -

<?xml version="1.0" encoding="UTF-8"?>
<tns:Employees xmlns:tns="http://www.example.org/Employees">
    <tns:Employee>
        <tns:Name>tns:Name</tns:Name>
        <tns:ID>tns:ID</tns:ID>
        <tns:Designation>tns:Designation</tns:Designation>
        <tns:Dept1>tns:Dept</tns:Dept1>
    </tns:Employee>
</tns:Employees>

I get the output -
not valid because cvc-complex-type.2.4.a: Invalid content was found starting with element 'tns:Dept1'. One of '{"http://www.example.org/Employees":Dept}' is expected.

So you see, the XML Parser pin points the exact error location along with the resolution. So for the above case it expects a <Dept> tag whereas what I pass is <Dept1>. Hence, the error.
   


Wednesday, May 9, 2012

Append Nodes to a an existing XML via XSL

Many a times we have a need to manipulate an xml to append similar repeating nodes to it. This post addresses that need through a very simply written xsl.
For the context what I have here is an Employees xml schema as below -
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Employees" xmlns:tns="http://www.example.org/Employees"  elementFormDefault="qualified">

<element name="Employees">
<complexType>
<sequence>
<element name="Employee" type="tns:empType" minOccurs="1" maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
<complexType name="empType">
<sequence>
<element name="Name" type="string" />
<element name="ID" type="string" />
<element name="Designation" type="string" />
<element name="Dept" type="string" />
</sequence>
</complexType>
</schema>


I have an xml corresponding to the above schema as shown below -

<?xml version="1.0" encoding="UTF-8"?>
<tns:Employees xmlns:tns="http://www.example.org/Employees"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<tns:Employee>
<tns:Name>Jack</tns:Name>
<tns:ID>1</tns:ID>
<tns:Designation>Developer</tns:Designation>
<tns:Dept>Java</tns:Dept>
</tns:Employee>
<tns:Employee>
<tns:Name>Bill</tns:Name>
<tns:ID>2</tns:ID>
<tns:Designation>Architect</tns:Designation>
<tns:Dept>SOA</tns:Dept>
</tns:Employee>
</tns:Employees>

All that I would be doing is copying the entire xml in its entirety and then appending an extra tag to it after the last one. The xsl for achieving this is as below -
<?xml version="1.0" encoding="UTF-8"?>


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tns="http://www.example.org/Employees"
exclude-result-prefixes="#default tns">

<xsl:template match="/">
<tns:Employees>
<xsl:for-each select="/tns:Employees/tns:Employee">
<xsl:copy-of select="." />
<xsl:if test="position()=last()">
<xsl:call-template name="append" /> </xsl:if>
</xsl:for-each>
</tns:Employees>
</xsl:template>
<xsl:template name="append">
<tns:Employee>
<tns:Name>Demo-Name</tns:Name>
<tns:ID>3</tns:ID>
<tns:Designation>Demo-Designation</tns:Designation>
<tns:Dept>Demo-Dept</tns:Dept>
</tns:Employee>
</xsl:template>
</xsl:stylesheet>


The template named 'append' contains the new <Employee> tag that needs to be appended.
So the output xml obtained after executing the above xsl with the Employees.xml as input is as below -

<?xml version="1.0" encoding="UTF-8"?>
<tns:Employees xmlns:tns="http://www.example.org/Employees">
<tns:Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:Name>Jack</tns:Name>
<tns:ID>1</tns:ID>
<tns:Designation>Developer</tns:Designation>
<tns:Dept>Java</tns:Dept>
</tns:Employee>
<tns:Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tns:Name>Bill</tns:Name>
<tns:ID>2</tns:ID>
<tns:Designation>Architect</tns:Designation>
<tns:Dept>SOA</tns:Dept>
</tns:Employee>
<tns:Employee>
<tns:Name>Demo-Name</tns:Name>
<tns:ID>3</tns:ID>
<tns:Designation>Demo-Designation</tns:Designation>
<tns:Dept>Demo-Dept</tns:Dept>
</tns:Employee>
</tns:Employees>
You see the new <Employee> appended right after the last <Employee> tag of the original xml.Hope it helps! 


Do let me know if you face problems with xml manipulation using xsl. I'll be happy to help :)