Skip to content
This repository has been archived by the owner on Nov 22, 2019. It is now read-only.

Commit

Permalink
Added methods to view model object
Browse files Browse the repository at this point in the history
- added methods in view model for adding and removing sources
- fixed some lint errors
  • Loading branch information
elvisisking committed Jun 22, 2018
1 parent f00e8ff commit 2193e81
Show file tree
Hide file tree
Showing 13 changed files with 559 additions and 89 deletions.
80 changes: 74 additions & 6 deletions ngapp/src/app/dataservices/shared/view.model.ts
Expand Up @@ -25,7 +25,6 @@ export class View {
private keng__id: string;
private description: string;
private isSelected = false;
private isValid = false;
private isEditable = false;
private sources: SchemaNode[] = [];

Expand Down Expand Up @@ -65,7 +64,7 @@ export class View {
}

/**
* @param {string} name the view description
* @param {string} description the view description
*/
public setDescription( description?: string ): void {
this.description = description ? description : null;
Expand All @@ -81,7 +80,7 @@ export class View {
/**
* @param {SchemaNode[]} sources the view sources
*/
public setSources( sources: SchemaNode[] ): void {
public setSources( sources: SchemaNode[] = [] ): void {
this.sources = sources;
}

Expand All @@ -93,10 +92,10 @@ export class View {
// The view currently supports single source only
let sourceNodeName = "unknownSource";
let connectionName = "unknownConnection";
const source = this.getSources()[0];
if (source !== null) {
const source = this.getSources()[ 0 ];
if ( source !== null ) {
sourceNodeName = source.getName();
if (source.getConnectionName() !== null) {
if ( source.getConnectionName() !== null ) {
connectionName = source.getConnectionName();
}
}
Expand All @@ -105,6 +104,75 @@ export class View {
return "SELECT * FROM " + connectionName.toLowerCase() + VdbsConstants.SCHEMA_MODEL_SUFFIX + "." + sourceNodeName + ";";
}

/**
* Duplicate sources are not added.
*
* @param {SchemaNode} sourceToAdd the source being added
*/
public addSource( sourceToAdd: SchemaNode ): void {
const index = this.sources.findIndex( ( source ) => source.getName() === sourceToAdd.getName() );

if ( index === -1 ) {
this.sources.push( sourceToAdd );
}
}

/**
* Duplicate sources are not added.
*
* @param {SchemaNode[]} sourcesToAdd the sources being added
*/
public addSources( sourcesToAdd: SchemaNode[] = [] ): void {
const self = this;

sourcesToAdd.forEach( ( source ) => {
self.addSource( source );
} );
}

/**
* @param {SchemaNode | string} sourceToRemove the source or the ID of the source being removed
*/
public removeSource( sourceToRemove: SchemaNode | string ): void {
let sourceName: string;

if ( typeof sourceToRemove === "string" ) {
sourceName = sourceToRemove as string;
} else {
const source = sourceToRemove as SchemaNode;
sourceName = source.getName();
}

const index = this.sources.findIndex( ( source ) => source.getName() === sourceName );

if ( index !== -1 ) {
this.sources.splice( index, 1 );
}
}

/**
* @param {SchemaNode} sourcesToRemove the sources being removed
*/
public removeSources( sourcesToRemove: SchemaNode[] | string[] ): void {
const self = this;

if ( sourcesToRemove && sourcesToRemove.length !== 0 ) {
if ( typeof sourcesToRemove[ 0 ] === "string" ) {
const ids = sourcesToRemove as string[];

ids.forEach( ( source ) => {
self.removeSource( source );
} );
} else {
const sources = sourcesToRemove as SchemaNode[];

sources.forEach( ( source ) => {
self.removeSource( source );
} );
}
}
}

/**
* Determine whether the view is in a valid state
* @returns {boolean} true if valid
Expand Down
@@ -0,0 +1,60 @@
/**
* @license
* Copyright 2017 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { ViewEditorI18n } from "@dataservices/virtualization/view-editor/view-editor-i18n";
import { Command } from "@dataservices/virtualization/view-editor/command/command";

export class AddSourcesCommand extends Command {

/**
* The command identifier.
*
* @type {string}
*/
public static readonly id = "AddSourcesCommand";

/**
* The name of the command argument whose value is the IDs of the sources that are being added.
*
* @type {string}
*/
public static readonly addedSourcesIds = "addedSourcesIds";

/**
* @param {string} addedSourcesIds the IDs of the sources being added (cannot be `null` or empty)
*/
public constructor( addedSourcesIds: string[] ) {
super( AddSourcesCommand.id, ViewEditorI18n.addSourcesCommandName );

// concatenate IDs using comma as delimiter
let args = "";
let firstTime = true;

addedSourcesIds.forEach( ( id ) => {
if ( firstTime ) {
firstTime = false;
} else {
args += Command.idsDelimiter;
}

args += id;
} );

this._args.set( AddSourcesCommand.addedSourcesIds, args );
}

}
Expand Up @@ -3,11 +3,15 @@ import { UpdateViewNameCommand } from "@dataservices/virtualization/view-editor/
import { RemoveSourceCommand } from "@dataservices/virtualization/view-editor/command/remove-source-command";
import { UpdateViewDescriptionCommand } from "@dataservices/virtualization/view-editor/command/update-view-description-command";
import { CommandFactory } from "@dataservices/virtualization/view-editor/command/command-factory";
import { AddSourcesCommand } from "@dataservices/virtualization/view-editor/command/add-sources-command";
import { RemoveSourcesCommand } from "@dataservices/virtualization/view-editor/command/remove-sources-command";
import { NoOpCommand } from "@dataservices/virtualization/view-editor/command/no-op-command";

describe( "Command Factory Tests", () => {

it("AddSourceCommand Test", () => {
const cmd = CommandFactory.createAddSourceCommand( "theNewSource" );
expect( cmd.id ).toBe( AddSourceCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 1 );
expect( cmd.getArg( AddSourceCommand.addedSourceId ) ).toEqual( "theNewSource" );
Expand All @@ -31,8 +35,47 @@ describe( "Command Factory Tests", () => {
expect( cmd.undoCommand.getArg( RemoveSourceCommand.removedSourceId ) ).toEqual( cmd.getArg( AddSourceCommand.addedSourceId ) );
});

it("AddSourcesCommand Test", () => {
const sourcesIds: string[] = [ "a", "b", "c", "d" ];
const cmd = CommandFactory.createAddSourcesCommand( sourcesIds );
expect( cmd.id ).toBe( AddSourcesCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 1 );
expect( cmd.getArg( AddSourcesCommand.addedSourcesIds ) ).toEqual( "a,b,c,d" );
expect( cmd.toString() ).toBe( "AddSourcesCommand, addedSourcesIds=a,b,c,d" );
expect( cmd.canUndo() ).toBe( false );
expect( cmd.undoCommand ).toBeNull();

const json = cmd.toJSON();
const roundtrip = CommandFactory.decode( json );
expect( roundtrip.id ).toBe( cmd.id );
expect( roundtrip.args.size ).toBe( cmd.args.size );
expect( roundtrip.getArg( AddSourcesCommand.addedSourcesIds ) ).toBe( cmd.getArg( AddSourcesCommand.addedSourcesIds ) );
});

it("AddSourcesCommand Undo Test", () => {
const sourcesIds: string[] = [ "a", "b", "c", "d" ];
const cmd = CommandFactory.createAddSourcesCommand( sourcesIds, true );
expect( cmd.canUndo() ).toBe( true );
expect( cmd.undoCommand ).not.toBeNull();
expect( cmd.undoCommand.id ).toBe( RemoveSourcesCommand.id );
expect( cmd.undoCommand.args.size ).toBe( 1 );
expect( cmd.undoCommand.getArg( RemoveSourcesCommand.removedSourcesIds ) ).toEqual( cmd.getArg( AddSourcesCommand.addedSourcesIds ) );
});

it("NoOpCommand Test", () => {
const cmd = CommandFactory.createNoOpCommand();
expect( cmd.id ).toBe( NoOpCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 0 );
expect( cmd.toString() ).toEqual( "NoOpCommand, []" );
expect( cmd.canUndo() ).toBe( false );
expect( cmd.undoCommand ).toBeNull();
});

it("RemoveSourceCommand Test", () => {
const cmd = CommandFactory.createRemoveSourceCommand( "theRemovedSource" );
expect( cmd.id ).toBe( RemoveSourceCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 1 );
expect( cmd.getArg( RemoveSourceCommand.removedSourceId ) ).toEqual( "theRemovedSource" );
Expand All @@ -56,9 +99,38 @@ describe( "Command Factory Tests", () => {
expect( cmd.undoCommand.getArg( AddSourceCommand.addedSourceId ) ).toEqual( cmd.getArg( RemoveSourceCommand.removedSourceId ) );
});

it("RemoveSourcesCommand Test", () => {
const sourcesIds: string[] = [ "a", "b", "c", "d" ];
const cmd = CommandFactory.createRemoveSourcesCommand( sourcesIds );
expect( cmd.id ).toBe( RemoveSourcesCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 1 );
expect( cmd.getArg( RemoveSourcesCommand.removedSourcesIds ) ).toEqual( "a,b,c,d" );
expect( cmd.toString() ).toEqual( "RemoveSourcesCommand, removedSourcesIds=a,b,c,d" );
expect( cmd.canUndo() ).toBe( false );
expect( cmd.undoCommand ).toBeNull();

const json = cmd.toJSON();
const roundtrip = CommandFactory.decode( json );
expect( roundtrip.id ).toBe( cmd.id );
expect( roundtrip.args.size ).toBe( cmd.args.size );
expect( roundtrip.getArg( RemoveSourcesCommand.removedSourcesIds ) ).toBe( cmd.getArg( RemoveSourcesCommand.removedSourcesIds ) );
});

it("RemoveSourcesCommand Undo Test", () => {
const sourcesIds: string[] = [ "a", "b", "c", "d" ];
const cmd = CommandFactory.createRemoveSourcesCommand( sourcesIds, true );
expect( cmd.canUndo() ).toBe( true );
expect( cmd.undoCommand ).not.toBeNull();
expect( cmd.undoCommand.id ).toBe( AddSourcesCommand.id );
expect( cmd.undoCommand.args.size ).toBe( 1 );
expect( cmd.undoCommand.getArg( AddSourcesCommand.addedSourcesIds ) ).toEqual( cmd.getArg( RemoveSourcesCommand.removedSourcesIds ) );
});

it("UpdateViewDescriptionCommand Test", () => {
const cmd = CommandFactory.createUpdateViewDescriptionCommand( "theNewDescription",
"theOldDescription" );
expect( cmd.id ).toBe( UpdateViewDescriptionCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 2 );
expect( cmd.getArg( UpdateViewDescriptionCommand.newDescription ) ).toEqual( "theNewDescription" );
Expand Down Expand Up @@ -93,6 +165,7 @@ describe( "Command Factory Tests", () => {

it("UpdateViewNameCommand Test", () => {
const cmd = CommandFactory.createUpdateViewNameCommand( "theNewName", "theOldName" );
expect( cmd.id ).toBe( UpdateViewNameCommand.id );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 2 );
expect( cmd.getArg( UpdateViewNameCommand.newName ) ).toEqual( "theNewName" );
Expand Down

0 comments on commit 2193e81

Please sign in to comment.