All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmdx.application.mof.mapping.java.QueryMapper Maven / Gradle / Ivy

There is a newer version: 2.18.10
Show newest version
/*
 * ====================================================================
 * Project:     openmdx, http://www.openmdx.org/
 * Description: JMI Query Mapper
 * Owner:       OMEX AG, Switzerland, http://www.omex.ch
 * ====================================================================
 *
 * This software is published under the BSD license
 * as listed below.
 * 
 * Copyright (c) 2006-2011, OMEX AG, Switzerland
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 * 
 * * Neither the name of the openMDX team nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * ------------------
 * 
 * This product includes or is based on software developed by other 
 * organizations as listed in the NOTICE file.
 */

package org.openmdx.application.mof.mapping.java;

import java.io.Writer;
import java.util.Iterator;

import org.omg.mof.spi.Identifier;
import org.openmdx.application.mof.mapping.cci.ClassifierDef;
import org.openmdx.application.mof.mapping.cci.MetaData_1_0;
import org.openmdx.application.mof.mapping.cci.ReferenceDef;
import org.openmdx.application.mof.mapping.cci.StructuralFeatureDef;
import org.openmdx.application.mof.mapping.spi.MapperUtils;
import org.openmdx.base.exception.ServiceException;
import org.openmdx.base.mof.cci.Model_1_0;
import org.openmdx.base.mof.cci.Multiplicity;

/**
 * JMI Query Mapper
 */
public class QueryMapper
    extends AbstractMapper {

    /**
     * Constructor 
     *
     * @param writer
     * @param model
     * @param format 
     * @param packageSuffix
     * @param metaData 
     * @param primitiveTypeMapper 
     */
    public QueryMapper(
        Writer writer,
        Model_1_0 model,
        Format format, 
        String packageSuffix, 
        MetaData_1_0 metaData, 
        PrimitiveTypeMapper primitiveTypeMapper
    ) {
        super(
            writer,
            model,
            format, 
            packageSuffix,
            metaData, 
            primitiveTypeMapper
        );
    }
    
    /**
     * @param classDef
     * @param featureDef
     * @throws ServiceException
     */
    public void mapStructuralFeature(
        ClassifierDef classDef,
        StructuralFeatureDef featureDef
    ) throws ServiceException {
        this.trace("Query/Feature");
        String queryType = getQueryType(featureDef.getQualifiedTypeName(), null); 
        boolean qualifiedReference =
            featureDef instanceof ReferenceDef &&
            ((ReferenceDef)featureDef).getQualifiedQualifierTypeName() != null;
        if(
            Multiplicity.SINGLE_VALUE.toString().equals(featureDef.getMultiplicity()) &&
            !qualifiedReference
        ) {
            //
            // single-valued mandatory feature
            //
            this.pw.println("  /**");
            MapperUtils.wrapText(
                "   * ",
                "Adds a constraint for the feature " + featureDef.getName() + " to the predicate. The predicate for " + 
                classDef.getName() + " evaluates true if its value of feature " + featureDef.getName() + 
                " satisfies the selected condition."
            );
            this.pw.println("   */");
            this.pw.println("  public " + queryType + ' ' + getPredicateName(null, featureDef) + '(');
            this.pw.println("  );");
            this.pw.println();
        } else {
            //
            // optional or multi-valued feature
            //
            if(
                Multiplicity.OPTIONAL.toString().equals(featureDef.getMultiplicity()) &&
                !qualifiedReference
             ) {
                //
                // is null?
                // 
                this.pw.println("  /**");
                this.pw.println(
                    MapperUtils.wrapText(
                        "   * ",
                        "Adds a constraint to the predicate for " + classDef.getName() + 
                        " testing whether the value of the feature " + featureDef.getName() + 
                        " is null or not."
                    )
                );
                this.pw.println("   */");
                this.pw.println("  public org.w3c.cci2.OptionalFeaturePredicate " + getPredicateName(null, featureDef) + '(');
                this.pw.println("  );");
                this.pw.println();
            } else {
                //
                // is empty?
                // 
                this.pw.println("  /**");
                this.pw.println(
                    MapperUtils.wrapText(
                        "   * ",
                        "Adds a constraint to the predicate for " + classDef.getName() + 
                        " testing whether the feature " + featureDef.getName() + 
                        " has values or not."
                    )
                );
                this.pw.println("   */");
                this.pw.println("  public org.w3c.cci2.MultivaluedFeaturePredicate " + getPredicateName(null, featureDef) + '(');
                this.pw.println("  );");
                this.pw.println();
            }
            //
            // there exists
            //
            this.pw.println("  /**");
            if (
                Multiplicity.OPTIONAL.code().equals(featureDef.getMultiplicity())
            ) {
                this.pw.println(
                    MapperUtils.wrapText(
                        "   * ",
                        "Adds a condition for the feature " + featureDef.getName() + " to the predicate for " + 
                        classDef.getName() + ", which evaluates to false unless the value of the feature " + 
                        featureDef.getName() + " satisfies the given condition."
                    )
                );
                this.pw.println(
                    MapperUtils.wrapText(
                        "   * ",
                        "

Since the attribute is optional its value may be null, " + "in which case the condition is not satisfied" ) ); } else { this.pw.println( MapperUtils.wrapText( " * ", "Adds a condition for the feature " + featureDef.getName() + " to the predicate for " + classDef.getName() + ", which evaluates to false unless the values of the feature " + featureDef.getName() + " satisfy the given condition." ) ); this.pw.println( MapperUtils.wrapText( " * ", "

Since the multiplicity for this attribute is 0..n, the attribute may have no values. " + "in which case the condition is not satisfied" ) ); } this.pw.println(" */"); this.pw.println(" public " + queryType + ' ' + getPredicateName("thereExists", featureDef) + '('); this.pw.println(" );"); this.pw.println(); // // for all // this.pw.println(" /**"); if ( Multiplicity.OPTIONAL.code().equals(featureDef.getMultiplicity()) ) { this.pw.println( MapperUtils.wrapText( " * ", "Adds a condition for the feature " + featureDef.getName() + " to the predicate for " + classDef.getName() + ", which evaluates to false unless the value of the feature " + featureDef.getName() + " satisfies the given condition." ) ); this.pw.println( MapperUtils.wrapText( " * ", "

Since the attribute is optional its value may be null, " + "in which case the condition is satisfied" ) ); } else { this.pw.println( MapperUtils.wrapText( " * ", "Adds a condition for the feature " + featureDef.getName() + " to the predicate for " + classDef.getName() + ", which evaluates to false unless the values of the feature " + featureDef.getName() + " satisfy the given condition." ) ); this.pw.println( MapperUtils.wrapText( " * ", "

Since the multiplicity for this attribute is 0..n, the attribute may have no values. " + "in which case the condition is satisfied" ) ); } this.pw.println(" */"); this.pw.println(" public " + queryType + ' ' + getPredicateName("forAll", featureDef) + '('); this.pw.println(" );"); this.pw.println(); } if(this.model.isPrimitiveType(featureDef.getQualifiedTypeName())) { String orderType = getOrderType(featureDef); if(orderType != null) { // // order by // this.pw.println(" /**"); this.pw.println( MapperUtils.wrapText( " * ", "Allows to adds a sort instruction for " + classDef.getName() + " depending the feature " + featureDef.getName() + "" ) ); this.pw.println( MapperUtils.wrapText( " * ", "Note: The order in which orderBy… instructions are given is relevant!" ) ); this.pw.println(" */"); this.pw.println(" public " + orderType + ' ' + getPredicateName("orderBy", featureDef) + '('); this.pw.println(" );"); this.pw.println(); } } } /** * */ public void mapEnd( ) { this.trace("Query/End"); this.pw.println("} "); } /** * * @param classifierDef * @throws ServiceException */ public void mapBegin( ClassifierDef classifierDef ) throws ServiceException { this.trace("Query/Begin"); this.fileHeader(); String qualifiedName = classifierDef.getQualifiedName(); this.pw.println( "package " + this.getNamespace( MapperUtils.getNameComponents( MapperUtils.getPackageName(qualifiedName) ) ) + ';' ); this.pw.println(); this.pw.println("/**"); MapperUtils.wrapText( " * ", "A " + classifierDef.getName() + "Query selects a set of instances of class " + classifierDef.getName() + " based on conditions to be met by their attributes. " ); this.pw.println(" */"); this.pw.println("public interface " + Identifier.CLASS_PROXY_NAME.toIdentifier(MapperUtils.getElementName(qualifiedName), null, null,null, "Query")); this.pw.print(" extends "); if(classifierDef.getSupertypes().isEmpty()) { this.pw.println("org.w3c.cci2.AnyTypePredicate"); } else { String prefix = ""; for ( Iterator i = classifierDef.getSupertypes().iterator(); i.hasNext(); prefix = ",\n " ) { ClassifierDef supertypeDef = (ClassifierDef) i.next(); this.pw.print( prefix + getNamespace( MapperUtils.getNameComponents(MapperUtils.getPackageName(supertypeDef.getQualifiedName())) ) + '.' + Identifier.CLASS_PROXY_NAME.toIdentifier(supertypeDef.getName(), null, null, null, "Query") ); } this.pw.println(); } this.pw.println("{"); this.pw.println(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy