10 動的問合せの作成
動的問合せを作成するには、Oracle OLAP Java API Templateクラスおよびその他の関連クラスを使用します。 次のトピックでは、これらのクラスについて説明し、それらの実装の例を示します。
10.1 Templateオブジェクトについて
Templateクラスは、Oracle OLAP Java APIの非常に強力な機能の基礎です。 Templateオブジェクトを使用して、変更可能なSourceオブジェクトを作成します。 これらのSourceオブジェクトを使用すると、エンド・ユーザーの選択に応じて変更できる動的問合せを作成できます。 また、Templateオブジェクトを使用すると、ユーザー・インタフェース要素をOLAP Java API操作およびオブジェクトに変換できます。
動的Sourceの変更、およびそれらの変更の保存または破棄に使用するTransactionオブジェクトの詳細は、「TransactionProviderの使用」を参照してください。
10.1.1 動的Sourceの作成
Templateの主な機能は、動的Sourceを生成する機能です。 この機能は、Templateが使用する他の2つのオブジェクトに基づいています: DynamicDefinitionおよびMetadataStateクラスのインスタンス。
Sourceが作成されると、Oracle OLAPは自動的にSourceDefinitionをそれに関連付けます。 SourceDefinitionには、Sourceに関する情報があります。 作成後は、Sourceおよび関連するSourceDefinitionが非常に関連付けられます。 SourceDefinitionのgetSourceメソッドは、それに関連付けられたSourceを返します。
DynamicDefinitionは、SourceDefinitionのサブクラスです。 Templateは、Templateによって生成されるSourceのSourceDefinitionのプロキシとして機能するDynamicDefinitionを作成します。 つまり、DynamicDefinitionのgetSourceメソッドは、常に同じ不適切に関連付けられたSourceを取得するのではなく、Templateによって現在作成されているSourceを取得します。 DynamicDefinitionのインスタンスは、取得されるSourceが異なる場合でも変更されません。
Templateが生成するSourceは、TemplateがSourceの作成に使用する他のSourceオブジェクトを含む値が変更される可能性があるため、変更される可能性があります。 Templateは、これらの値をMetadataStateに格納します。 Templateは、MetadataStateの現在の状態を取得し、値を取得または設定し、状態を設定するメソッドを提供します。 これらのメソッドを使用して、MetadataStateが格納するデータ値を変更します。
DynamicDefinitionを使用して、Templateによって生成されたSourceを取得します。 アプリケーションでTemplateがSourceの作成に使用する値の状態を変更した場合(たとえば、エンド・ユーザーの選択に応じて)、新しいSourceが前のSourceとは異なる結果セットを定義している場合でも、アプリケーションは同じDynamicDefinitionを使用してSourceを再度取得します。
Templateによって生成されるSourceは、一連の選択、ソート、計算、結合など、他のSourceオブジェクトを作成する一連のSource操作の結果です。 これらの操作のコードは、TemplateのSourceGeneratorのgenerateSourceメソッドに配置します。 このメソッドは、Templateによって生成されたSourceを返します。 この操作では、MetadataStateに格納されているデータが使用されます。
多数の異なるTemplateオブジェクトによって生成される動的Sourceオブジェクトの相互作用を含む非常に複雑な問合せをビルドできます。 問合せビルドの最終結果は、複雑な問合せ全体を定義するSourceです。 最終的なSourceの作成に使用したTemplateオブジェクトの状態を変更した場合、最後のSourceは前のSourceとは異なる結果セットを表します。 これによって、問合せを定義するために行ったすべての操作を繰り返さずに、最終の問合せを変更できます。
親トピック: テンプレート・オブジェクトについて
10.1.2 ユーザー・インタフェース要素のOLAP Java APIオブジェクトへの変換
Templateオブジェクトは、アプリケーションのユーザー・インタフェースの要素を表すように設計します。 Templateオブジェクトは、エンド・ユーザーが行った選択を、Sourceを生成するOLAP Java API問合せビルド操作に変換します。 次に、Cursorを作成して、Sourceで定義された結果セットをOracle OLAPからフェッチします。 Cursorから値を取得し、エンド・ユーザーに表示します。 エンド・ユーザーが選択を変更すると、Templateの状態が変更されます。 次に、Templateによって生成されたSourceを取得し、新しいCursorを作成し、新しい値を取得して表示します。
親トピック: テンプレート・オブジェクトについて
10.2 Templateおよび関連クラスの概要
OLAP Java APIでは、複数のクラスが連携して動的なSourceを生成します。 Templateの設計では、次のものを実装または拡張する必要があります:
-
Template抽象クラス -
MetadataStateインタフェース -
SourceGeneratorインタフェース
これらの3つのクラスのインスタンス、およびDataProviderクラスとDynamicDefinitionクラスのインスタンスが連携して、Templateが定義するSourceを生成します。
- 動的Sourceを作成するクラス間の関連
- Templateクラス
- 「MetadataStateインタフェース」
- 「SourceGeneratorインタフェース」
- 「DynamicDefinitionクラス」
親トピック: 動的問合せの作成
10.2.1 動的Sourceを作成するクラス間の関連
動的Sourceを生成するクラスは次のように連携して動作します:
-
Templateには、DynamicDefinitionを作成し、MetadataStateの現在の状態を取得および設定するメソッドがあります。Template抽象クラスの拡張により、MetadataStateのフィールドの値を取得および設定するメソッドが追加されます。 -
MetadataState実装には、TemplateのSourceの生成に使用するデータを格納するためのフィールドがあります。 新しいTemplateを作成する場合、MetadataStateをTemplateのコンストラクタに渡します。DynamicDefinitionのgetSourceメソッドをコールすると、MetadataStateはSourceGeneratorのgenerateSourceメソッドに渡されます。 -
DataProviderは、Templateの作成に使用され、SourceGeneratorが新しいSourceオブジェクトの作成に使用されます。 -
SourceGenerator実装には、MetadataState内のデータの現在の状態を使用してTemplateのSourceを生成するgenerateSourceメソッドがあります。SourceGeneratorをTemplateのcreateDynamicDefinitionメソッドに渡して、DynamicDefinitionを作成します。 -
DynamicDefinitionには、SourceGeneratorによって生成されたSourceを取得するgetSourceメソッドがあります。DynamicDefinitionは、SourceDefinitionのプロキシとして機能します。このプロキシは、Sourceと非常に関連付けられています。
親トピック: テンプレートおよび関連クラスの概要
10.2.2 Templateクラス
Templateを使用して、変更可能なSourceを生成します。 Templateには、DynamicDefinitionを作成し、Templateの現在の状態を取得および設定するためのメソッドがあります。 Templateクラスの拡張では、TemplateのMetadataStateのフィールドへのアクセスを提供するメソッドを追加します。 Templateは、TemplateのSourceGeneratorによって生成されたSourceを取得するために使用するDynamicDefinitionを作成します。
Template実装の例については、「例10-1」を参照してください。
親トピック: テンプレートおよび関連クラスの概要
10.2.3 MetadataStateインタフェース
MetadataStateインタフェースの実装には、Templateの値の現在の状態が格納されます。 MetadataStateには、現在の状態のコピーを作成するcloneメソッドが含まれている必要があります。
新しいTemplateをインスタンス化する場合、MetadataStateをTemplateコンストラクタに渡します。 Templateには、MetadataStateによって格納される値を取得および設定するためのメソッドがあります。 TemplateのSourceGeneratorのgenerateSourceメソッドは、メソッドがTemplateのSourceを生成するときにMetadataStateを使用します。
MetadataState実装の例については、「例10-2」を参照してください。
親トピック: テンプレートおよび関連クラスの概要
10.2.4 SourceGeneratorインタフェース
SourceGeneratorの実装には、TemplateのSourceを生成するgenerateSourceメソッドが含まれている必要があります。 SourceGeneratorは、BooleanSource、NumberSource、StringSourceなどの1つのタイプのSourceのみを生成する必要があります。 Sourceの生成では、generateSourceメソッドは、TemplateのMetadataStateで表されるデータの現在の状態を使用します。
generateSourceメソッドによって生成されたSourceを取得するには、SourceGeneratorをTemplateのcreateDynamicDefinitionメソッドに渡して、DynamicDefinitionを作成します。 次に、DynamicDefinitionのgetSourceメソッドをコールして、Sourceを取得します。
Templateは複数のDynamicDefinitionを作成でき、それぞれに異なる実装のSourceGeneratorがあります。 異なるSourceGeneratorオブジェクトのgenerateSourceメソッドは、TemplateのMetadataStateの現在の状態によって定義される同じデータを使用して、異なる問合せを定義するSourceオブジェクトを生成します。
SourceGenerator実装の例については、「例10-3」を参照してください。
親トピック: テンプレートおよび関連クラスの概要
10.2.5 DynamicDefinitionクラス
DynamicDefinitionは、SourceDefinitionのサブクラスです。 DynamicDefinitionを作成するには、TemplateのcreateDynamicDefinitionメソッドをコールし、SourceGeneratorを渡します。 DynamicDefinitionのgetSourceメソッドをコールして、SourceGeneratorによって生成されたSourceを取得します。
Templateによって作成されるDynamicDefinitionは、SourceGeneratorによって生成されるSourceのSourceDefinitionのプロキシです。 SourceDefinitionは、Sourceと非常に関連付けられています。 Templateの状態が変更された場合、SourceGeneratorによって生成されるSourceは異なります。 DynamicDefinitionはプロキシであるため、SourceのSourceDefinitionが異なる場合でも、同じDynamicDefinitionを使用して新しいSourceを取得します。
DynamicDefinitionのgetCurrentメソッドは、generateSourceメソッドが現在返すSourceと非常に関連しているSourceDefinitionを返します。 DynamicDefinitionの使用例については、「例10-4」を参照してください。
親トピック: テンプレートおよび関連クラスの概要
10.3 Templateの設計および実装
Templateの設計には、アプリケーションのユーザー・インタフェースの問合せビルド要素が反映されます。 たとえば、エンド・ユーザーが値リストから多くの値を要求する問合せを作成可能なアプリケーションを開発すると想定します。 値は、メジャーの1つのディメンションの値です。 そのメジャーの他のディメンションは、単一の値に制限されます。
アプリケーションのユーザー・インタフェースでは、エンド・ユーザーがダイアログ・ボックスを使用して次の操作を実行できます。
-
データ値が特定の範囲内にあるかどうかを指定するラジオ・ボタンの選択。
-
ドロップダウン・リストからのメジャーの選択。
-
フィールドからの数値の選択。 この数値によって、表示するデータ値の数を指定します。
-
表示するデータ値のベースとしての、メジャーのディメンションの選択。 たとえば、ユーザーが製品ディメンションを選択した場合、問合せによって、製品リストからいくつかの製品が指定されます。 リストは、メジャー、および他のディメンションの選択値によって決定されます。
-
エンド・ユーザーが選択したメジャーの他のディメンションの単一値を選択するダイアログ・ボックスを開くには、ボタンをクリックします。 ディメンションの値を選択すると、エンド・ユーザーは2番目のダイアログ・ボックスで「OK」ボタンをクリックし、最初のダイアログ・ボックスに戻ります。
-
「OK」ボタンをクリックし、問合せを作成します。 問合せ結果が表示されます。
エンド・ユーザーが最初のダイアログ・ボックスで作成する問合せを表すSourceを生成するには、TopBottomTemplateというTemplateを設計します。 また、SingleSelectionTemplateという2番目のTemplateを設計して、基本ディメンション以外のディメンションの単一値のエンド・ユーザーの選択を表すSourceを作成します。 Templateオブジェクトの設計には、ダイアログ・ボックスのユーザー・インタフェース要素が反映されます。
TopBottomTemplateとそのMetadataStateおよびSourceGeneratorの設計では、次の操作を行います:
-
Templateを拡張するTopBottomTemplateというクラスを作成します。 クラスに、Templateの現在の状態を取得するメソッドを追加し、ユーザーが指定した値を設定してから、Templateの現在の状態を設定します。 -
MetadataStateを実装するTopBottomTemplateStateというクラスを作成します。Templateによって生成されるSourceの生成に使用するSourceGeneratorの値を格納するフィールドをクラスに指定します。 値は、TopBottomTemplateのメソッドによって設定されます。 -
SourceGeneratorを実装するTopBottomTemplateGeneratorというクラスを作成します。 クラスのgenerateSourceメソッドで、エンド・ユーザーの選択によって指定されたSourceを作成する操作を指定します。
エンド・ユーザーは、このアプリケーションを使用して、最初のダイアログ・ボックスで販売台数をメジャーとして選択し、製品をベース・ディメンションとして選択します。 また、エンド・ユーザーは、残りの各ディメンションの単一の値としてAsia Pacific地域、2001年の第1四半期および直販チャネルも選択します。
エンド・ユーザーが作成した問合せは、2001年にAsia Pacific地域の顧客に対して直販チャネルで販売された製品のうち、販売台数の総数が高い上位10の製品を要求します。
TopBottomTemplate、TopBottomTemplateStateおよびTopBottomTemplateGeneratorクラスの実装の例、およびそれらを使用するアプリケーションの例については、「例10-1」、「例10-2」、「例10-3」 ,および「例10-4」を参照してください。 TopBottomTemplateStateおよびTopBottomTemplateGeneratorクラスは、TopBottomTemplate外部クラスの内部クラスとして実装されます。
10.3.1 Templateのクラスの実装
Template、MetadataStateおよびSourceGeneratorクラスを実装しています。
例10-1 Templateの実装
この例では、TopBottomTemplateクラスの実装です。
import oracle.olapi.data.source.DataProvider;
import oracle.olapi.data.source.DynamicDefinition;
import oracle.olapi.data.source.Source;
import oracle.olapi.data.source.SourceGenerator;
import oracle.olapi.data.source.Template;
import oracle.olapi.transaction.metadataStateManager.MetadataState;
/**
* Creates a TopBottomTemplateState, a TopBottomTemplateGenerator,
* and a DynamicDefinition.
* Gets the current state of the TopBottomTemplateState and the values
* that it stores.
* Sets the data values stored by the TopBottomTemplateState and sets the
* changed state as the current state.
*/
public class TopBottomTemplate extends Template
{
// Constants for specifying the selection of elements from the
// beginning or the end of the result set.
public static final int TOP_BOTTOM_TYPE_TOP = 0;
public static final int TOP_BOTTOM_TYPE_BOTTOM = 1;
// Variable to store the DynamicDefinition.
private DynamicDefinition dynamicDef;
/**
* Creates a TopBottomTemplate with a default type and number values
* and the specified base dimension.
*/
public TopBottomTemplate(Source base, DataProvider dataProvider)
{
super(new TopBottomTemplateState(base, TOP_BOTTOM_TYPE_TOP, 0),
dataProvider);
// Create the DynamicDefinition for this Template. Create the
// TopBottomTemplateGenerator that the DynamicDefinition uses.
dynamicDef =
createDynamicDefinition(new TopBottomTemplateGenerator(dataProvider));
}
/**
* Gets the Source produced by the TopBottomTemplateGenerator
* from the DynamicDefinition.
*/
public final Source getSource()
{
return dynamicDef.getSource();
}
/**
* Gets the Source that is the base of the elements in the result set.
* Returns null if the state has no base.
*/
public Source getBase()
{
TopBottomTemplateState state = (TopBottomTemplateState) getCurrentState();
return state.base;
}
/**
* Sets a Source as the base.
*/
public void setBase(Source base)
{
TopBottomTemplateState state = (TopBottomTemplateState) getCurrentState();
state.base = base;
setCurrentState(state);
}
/**
* Gets the Source that specifies the measure and the single
* selections from the dimensions other than the base.
*/
public Source getCriterion()
{
TopBottomTemplateState state = (TopBottomTemplateState) getCurrentState();
return state.criterion;
}
/**
* Specifies a Source that defines the measure and the single values
* selected from the dimensions other than the base.
* The SingleSelectionTemplate produces such a Source.
*/
public void setCriterion(Source criterion)
{
TopBottomTemplateState state = (TopBottomTemplateState) getCurrentState();
state.criterion = criterion;
setCurrentState(state);
}
/**
* Gets the type, which is either TOP_BOTTOM_TYPE_TOP or
* TOP_BOTTOM_TYPE_BOTTOM.
*/
public int getTopBottomType()
{
TopBottomTemplateState state = (TopBottomTemplateState) getCurrentState();
return state.topBottomType;
}
/**
* Sets the type.
*/
public void setTopBottomType(int topBottomType)
{
if ((topBottomType < TOP_BOTTOM_TYPE_TOP) ||
(topBottomType > TOP_BOTTOM_TYPE_BOTTOM ))
throw new IllegalArgumentException("InvalidTopBottomType");
TopBottomTemplateState state = (TopBottomTemplateState) getCurrentState();
state.topBottomType = topBottomType;
setCurrentState(state);
}
/**
* Gets the number of values selected.
*/
public float getN()
{
TopBottomTemplateState state = (TopBottomTemplateState) getCurrentState();
return state.N;
}
/**
* Sets the number of values to select.
*/
public void setN(float N)
{
TopBottomTemplateState state = (TopBottomTemplateState) getCurrentState();
state.N = N;
setCurrentState(state);
}
}
例10-2 MetadataStateの実装
この例では、TopBottomTemplateState内部クラスの実装です。
/**
* Stores data that can be changed by a TopBottomTemplate.
* The data is used by a TopBottomTemplateGenerator in producing
* a Source for the TopBottomTemplate.
*/
private static final class TopBottomTemplateState
implements Cloneable, MetadataState
{
public int topBottomType;
public float N;
public Source criterion;
public Source base;
/**
* Creates a TopBottomTemplateState.
*/
public TopBottomTemplateState(Source base, int topBottomType, float N)
{
this.base = base;
this.topBottomType = topBottomType;
this.N = N;
}
/**
* Creates a copy of this TopBottomTemplateState.
*/
public final Object clone()
{
try
{
return super.clone();
}
catch(CloneNotSupportedException e)
{
return null;
}
}
}
例10-3 SourceGeneratorの実装
この例では、TopBottomTemplateGenerator内部クラスの実装です。
/**
* Produces a Source for a TopBottomTemplate based on the data
* values of a TopBottomTemplateState.
*/
private final class TopBottomTemplateGenerator
implements SourceGenerator
{
// Store the DataProvider.
private DataProvider _dataProvider;
/**
* Creates a TopBottomTemplateGenerator.
*/
public TopBottomTemplateGenerator(DataProvider dataProvider)
{
_dataProvider = dataProvider;
}
/**
* Generates a Source for a TopBottomTemplate using the current
* state of the data values stored by the TopBottomTemplateState.
*/
public Source generateSource(MetadataState state)
{
TopBottomTemplateState castState = (TopBottomTemplateState) state;
if (castState.criterion == null)
throw new NullPointerException("CriterionParameterMissing");
Source sortedBase = null;
// Depending on the topBottomType value, select from the base Source
// the elements specified by the criterion Source and sort the
// elements in ascending or descending order.
// For descending order, specify that null values are last.
// For ascending order, specify that null values are first.
if (castState.topBottomType == TOP_BOTTOM_TYPE_TOP)
sortedBase = castState.base.sortDescending(castState.criterion, false);
else
sortedBase = castState.base.sortAscending(castState.criterion, true);
return sortedBase.interval(1, Math.round(castState.N));
}
}親トピック: テンプレートの設計と実装
10.3.2 Templateを使用するアプリケーションの実装
エンド・ユーザーによる選択内容をTemplateのMetadataStateに格納した後、DynamicDefinitionのgetSourceメソッドを使用して、Templateによって作成された動的Sourceを取得します。 このトピックでは、「例10-1」で説明されているTopBottomTemplateを使用するアプリケーションの例を示します。 便宜上、このコードでは多くの例外処理を省略しています。
BaseExample11gクラスは、Context11gクラスのインスタンスを作成および格納します。このクラスには、次のことを行うメソッドがあります:
-
コマンドライン引数で、ユーザーとしてOracle Databaseインスタンスに接続します。
-
Cursorオブジェクトを作成し、その値を表示します。
例10-4では、次の処理を実行します。
-
MdmMetadataProviderおよびMdmRootSchemaを取得します。 -
DataProviderを取得します。 -
ユーザーの
MdmDatabaseSchemaを取得します。 -
COSTS、UNITSおよびSALESメジャーを持つ
MdmCubeを取得します。 この例では、キューブから、UNITSメジャーとSALESメジャー、およびキューブに関連付けられたディメンションを取得します。 -
メジャーのディメンションの一部から単一の値を選択するための
SingleSelectionTemplateを作成します。 この例で使用するSingleSelectionTemplateクラスのコードについては、「SingleSelectionTemplateクラス」を参照してください。 -
TopBottomTemplateを作成し、エンド・ユーザーが行った選択を格納します。 -
TopBottomTemplateによって生成されたSourceを取得します。 -
Context11gオブジェクトを使用して、そのSourceのCursorを作成し、Cursor値を表示します。
「例7-3」の完全なコードには、「例10-4」にあるものと同じコードの一部が含まれています。 この例では、「例10-4」の先頭から次のコメントまで続くこのコードは示されていません:
// End of code not shown in //Example 7-3.
例10-4 Templateによって作成されたSourceの取得
import oracle.olapi.data.source.DataProvider;
import oracle.olapi.data.source.Source;
import oracle.olapi.examples.*;
import oracle.olapi.metadata.mdm.MdmAttribute;
import oracle.olapi.metadata.mdm.MdmBaseMeasure;
import oracle.olapi.metadata.mdm.MdmCube;
import oracle.olapi.metadata.mdm.MdmDatabaseSchema;
import oracle.olapi.metadata.mdm.MdmDimensionLevel;
import oracle.olapi.metadata.mdm.MdmDimensionMemberInfo;
import oracle.olapi.metadata.mdm.MdmHierarchyLevel;
import oracle.olapi.metadata.mdm.MdmLevelHierarchy;
import oracle.olapi.metadata.mdm.MdmMetadataProvider;
import oracle.olapi.metadata.mdm.MdmPrimaryDimension;
import oracle.olapi.metadata.mdm.MdmRootSchema;
/**
* Creates a query that specifies a number of elements from the top
* or bottom of a selection of dimension members, creates a Cursor
* for the query, and displays the values of the Cursor.
* The selected dimension members are those that have measure values
* that are specified by selected members of the other dimensions of
* the measure.
*/
public class TopBottomTest extends BaseExample11g
{
/**
* Gets the MdmMetadataProvider, the DataProvider, the MdmRootSchema, and the
* MdmDatabaseSchema for the current user.
* Gets the UNITS_CUBE_AWJ MdmCube.
* From the cube, gets the MdmBaseMeasure objects for the UNITS and SALES
* measures and the MdmPrimaryDimension objects that dimension them.
* Gets a hierarchy of the PRODUCT_AWJ dimension and the leaf level of the
* dimension.
* Gets the short description attribute of the dimension.
* Creates a SingleSelectionTemplate and adds selections to it.
* Creates a TopBottomTemplate and sets the properties of it.
* Gets the Source produced by the TopBottomTemplate, creates a Cursor
* for it, and displays the values of the Cursor.
* Changes the state of the SingleSelectionTemplate and the
* TopBottomTemplate, creates a new Cursor for the Source produced by the
* TopBottomTemplate, and displays the values of that Cursor.
*/
public void run() throws Exception
{
// Get the MdmMetadataProvider from the superclass.
MdmMetadataProvider metadataProvider = getMdmMetadataProvider();
// Get the DataProvider from the Context11g object of the superclass.
DataProvider dp = getContext().getDataProvider();
// Get the MdmRootSchema and the MdmDatabaseSchema for the user.
MdmRootSchema mdmRootSchema =
(MdmRootSchema)metadataProvider.getRootSchema();
MdmDatabaseSchema mdmDBSchema =
mdmRootSchema.getDatabaseSchema(getContext().getUser());
MdmCube unitsCube =
(MdmCube)mdmDBSchema.getTopLevelObject("UNITS_CUBE_AWJ");
MdmBaseMeasure mdmUnits = unitsCube.findOrCreateBaseMeasure("UNITS");
MdmBaseMeasure mdmSales = unitsCube.findOrCreateBaseMeasure("SALES");
// Get the Source objects for the measures.
Source units = mdmUnits.getSource();
Source sales = mdmSales.getSource();
// Get the MdmPrimaryDimension objects for the dimensions of the cube.
List<MdmPrimaryDimension> cubeDims = unitsCube.getDimensions();
MdmPrimaryDimension mdmTimeDim = null;
MdmPrimaryDimension mdmProdDim = null;
MdmPrimaryDimension mdmCustDim = null;
MdmPrimaryDimension mdmChanDim = null;
for(MdmPrimaryDimension mdmPrimDim : cubeDims)
{
if (mdmPrimDim.getName().startsWith("TIME"))
mdmTimeDim = mdmPrimDim;
else if (mdmPrimDim.getName().startsWith("PROD"))
mdmProdDim = mdmPrimDim;
else if (mdmPrimDim.getName().startsWith("CUST"))
mdmCustDim = mdmPrimDim;
else if (mdmPrimDim.getName().startsWith("CHAN"))
mdmChanDim = mdmPrimDim;
}
// Get the hierarchy of the PRODUCT_AWJ dimension.
MdmLevelHierarchy mdmProdHier =
mdmProdDim.findOrCreateLevelHierarchy("PRODUCT_PRIMARY");
// Get the detail dimenson level of the PRODUCT_AWJ dimension.
MdmDimensionLevel mdmItemDimLevel =
mdmProdDim.findOrCreateDimensionLevel("ITEM");
// Get the hierarchy level of the dimension level.
MdmHierarchyLevel mdmItemHierLevel =
mdmProdHier.findOrCreateHierarchyLevel(mdmItemDimLevel);
// Get the Source for the hierarchy level.
Source itemLevel = mdmItemHierLevel.getSource();
// Get the short description attribute for the PRODUCT_AWJ dimension and
// the Source for the attribute.
MdmAttribute mdmProdShortDescrAttr =
mdmProdDim.getShortValueDescriptionAttribute();
Source prodShortDescrAttr = mdmProdShortDescrAttr.getSource();
// Create a SingleSelectionTemplate to produce a Source that
// represents the measure values specified by single members of each of
// the dimensions of the measure other than the base dimension.
SingleSelectionTemplate singleSelections =
new SingleSelectionTemplate(units, dp);
// Create MdmDimensionMemberInfo objects for single members of the
// other dimensions of the measure.
MdmDimensionMemberInfo timeMemInfo =
new MdmDimensionMemberInfo(mdmTimeDim, "CALENDAR_YEAR::YEAR::CY2001");
MdmDimensionMemberInfo custMemInfo =
new MdmDimensionMemberInfo(mdmCustDim, "SHIPMENTS::REGION::APAC");
MdmDimensionMemberInfo chanMemInfo =
new MdmDimensionMemberInfo(mdmChanDim, "CHANNEL_PRIMARY::CHANNEL::DIR");
// Add the dimension member information objects to the
// SingleSelectionTemplate.
singleSelections.addDimMemberInfo(custMemInfo);
singleSelections.addDimMemberInfo(chanMemInfo);
singleSelections.addDimMemberInfo(timeMemInfo);
// Create a TopBottomTemplate specifying, as the base, the Source for a
// a level of a hierarchy.
TopBottomTemplate topNBottom = new TopBottomTemplate(itemLevel, dp);
// Specify whether to retrieve the elements from the beginning (top) or the
// end (bottom) of the selected elements of the base dimension.
topNBottom.setTopBottomType(TopBottomTemplate.TOP_BOTTOM_TYPE_TOP);
// Set the number of elements of the base dimension to retrieve.
topNBottom.setN(10);
// Get the Source produced by the SingleSelectionTemplate and specify it as
// the criterion object.
topNBottom.setCriterion(singleSelections.getSource());
// End of code not shown in
// Example 7-3.
// Display a description of the result.
String resultDescription = " products with the most units sold \nfor";
displayResultDescr(singleSelections, topNBottom, resultDescription);
// Get the Source produced by the TopBottomTemplate.
Source result = topNBottom.getSource();
// Join the Source produced by the TopBottomTemplate with the short
// value descriptions. Use the joinHidden method so that the
// dimension member values do not appear in the result.
Source result = prodShortDescrAttr.joinHidden(topNBottomResult);
// Commit the current transaction.
getContext().commit(); // Method of Context11g.
// Create a Cursor for the result and display the values of the Cursor.
getContext().displayTopBottomResult(result);
// Change a dimension member selection of the SingleSelectionTemplate.
timeMemInfo.setUniqueValue("CALENDAR_YEAR::YEAR::CY2000");
singleSelections.changeSelection(timeMemInfo);
// Change the number of elements selected and the type of selection.
topNBottom.setN(5);
topNBottom.setTopBottomType(TopBottomTemplate.TOP_BOTTOM_TYPE_BOTTOM);
// Join the Source produced by the TopBottomTemplate to the short
// description attribute.
result = prodShortDescrAttr.joinHidden(topNBottomResult);
// Commit the current transaction.
getContext().commit();
// Display a description of the result.
resultDescription = " products with the fewest units sold \nfor";
displayResultDescr(singleSelections, topNBottom, resultDescription);
// Create a new Cursor for the Source produced by the TopBottomTemplate
// and display the Cursor values.
getContext().displayTopBottomResult(result);
// Now change the measure to SALES, and get the top and bottom products by
// SALES.
singleSelections.setMeasure(sales);
// Change the number of elements selected.
topNBottom.setN(7);
// Change the type of selection back to the top.
topNBottom.setTopBottomType(TopBottomTemplate.TOP_BOTTOM_TYPE_TOP);
resultDescription = " products with the highest sales amounts \nfor";
displayResultDescr(singleSelections, topNBottom, resultDescription);
topNBottomResult = topNBottom.getSource();
result = prodShortDescrAttr.joinHidden(topNBottomResult);
// Commit the current transaction.
getContext().commit();
getContext().displayTopBottomResult(result);
// Change the type of selection back to the bottom.
topNBottom.setTopBottomType(TopBottomTemplate.TOP_BOTTOM_TYPE_BOTTOM);
resultDescription = " products with the lowest sales amounts \nfor";
displayResultDescr(singleSelections, topNBottom, resultDescription);
topNBottomResult = topNBottom.getSource();
result = prodShortDescrAttr.joinHidden(topNBottomResult);
// Commit the current transaction.
getContext().commit();
getContext().displayTopBottomResult(result);
}
/**
* Displays a description of the results of the query.
*
* @param singleSelections The SingleSelectionsTemplate used by the query.
*
* @param topNBottom The TopBottomTemplate used by the query.
*
* @param resultDescr A String that contains a description of the query.
*/
private void displayResultDescr(SingleSelectionTemplate singleSelections,
TopBottomTemplate topNBottom,
String resultDescr)
{
DataProvider dp = getContext().getDataProvider();
// Get the short descriptions of the dimension members of the
// SingleSelectionTemplate.
StringBuffer shortDescrsForMemberVals =
singleSelections.getMemberShortDescrs(dp);
// Display the number of dimension members selected, the result description,
// and the short descriptions of the single selection dimension members.
println("\nThe " + Math.round(topNBottom.getN()) + resultDescr
+ shortDescrsForMemberVals +" are:\n");
}
/**
* Runs the TopBottomTest application.
*
* @param args An array of String objects that provides the arguments
* required to connect to an Oracle Database instance, as
* specified in the Context11g class.
*/
public static void main(String[] args)
{
new TopBottomTest().execute(args);
}}
TopBottomTestプログラムは、次の出力を生成します。
The 10 products with the most units sold for Asia Pacific, Direct Sales, 2001 are: 1. Mouse Pad 2. Unix/Windows 1-user pack 3. Deluxe Mouse 4. Laptop carrying case 5. 56Kbps V.90 Type II Modem 6. 56Kbps V.92 Type II Fax/Modem 7. Keyboard Wrist Rest 8. Internal - DVD-RW - 6X 9. O/S Documentation Set - English 10. External - DVD-RW - 8X The 5 products with the fewest units sold for Asia Pacific, Direct Sales, 2000 are: 1. Envoy External Keyboard 2. O/S Documentation Set - Italian 3. External 48X CD-ROM 4. O/S Documentation Set - Spanish 5. Internal 48X CD-ROM USB The 7 products with the highest sales amounts for Asia Pacific, Direct Sales, 2000 are: 1. Sentinel Financial 2. Sentinel Standard 3. Envoy Executive 4. Sentinel Multimedia 5. Envoy Standard 6. Envoy Ambassador 7. 56Kbps V.90 Type II Modem The 7 products with the lowest sales amounts for Asia Pacific, Direct Sales, 2000 are: 1. Envoy External Keyboard 2. Keyboard Wrist Rest 3. Mouse Pad 4. O/S Documentation Set - Italian 5. O/S Documentation Set - Spanish 6. Standard Mouse 7. O/S Documentation Set - French
親トピック: テンプレートの設計と実装