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

Commit

Permalink
Initial cut at creating commands for view editor.
Browse files Browse the repository at this point in the history
- initial commands: add source, remove source, update view name, and update view description
- created factory to create commands and undo commands
  • Loading branch information
elvisisking committed Jun 18, 2018
1 parent 8d2932e commit 5aad1f4
Show file tree
Hide file tree
Showing 8 changed files with 717 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @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 AddSourceCommand extends Command {

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

/**
* The name of the command argument whose value is the identifier of the added source.
*
* @type {string}
*/
public static readonly addedSourceId = "addedSourceId";

/**
* @param {string} addedSourceId the ID of the source being added (cannot be `null` or empty)
*/
public constructor( addedSourceId: string ) {
super( AddSourceCommand.id, ViewEditorI18n.addSourceCommandName );
this._args.set( AddSourceCommand.addedSourceId, addedSourceId );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { AddSourceCommand } from "@dataservices/virtualization/view-editor/command/add-source-command";
import { UpdateViewNameCommand } from "@dataservices/virtualization/view-editor/command/update-view-name-command";
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";

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

it("AddSourceCommand Test", () => {
const cmd = CommandFactory.createAddSourceCommand( "theNewSource" );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 1 );
expect( cmd.getArg( AddSourceCommand.addedSourceId ) ).toEqual( "theNewSource" );
expect( cmd.toString() ).toBe( "AddSourceCommand, addedSourceId=theNewSource" );
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( AddSourceCommand.addedSourceId ) ).toBe( cmd.getArg( AddSourceCommand.addedSourceId ) );
});

it("AddSourceCommand Undo Test", () => {
const cmd = CommandFactory.createAddSourceCommand( "theNewSource", true );
expect( cmd.canUndo() ).toBe( true );
expect( cmd.undoCommand ).not.toBeNull();
expect( cmd.undoCommand.id ).toBe( RemoveSourceCommand.id );
expect( cmd.undoCommand.args.size ).toBe( 1 );
expect( cmd.undoCommand.getArg( RemoveSourceCommand.removedSourceId ) ).toEqual( cmd.getArg( AddSourceCommand.addedSourceId ) );
});

it("RemoveSourceCommand Test", () => {
const cmd = CommandFactory.createRemoveSourceCommand( "theRemovedSource" );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 1 );
expect( cmd.getArg( RemoveSourceCommand.removedSourceId ) ).toEqual( "theRemovedSource" );
expect( cmd.toString() ).toEqual( "RemoveSourceCommand, removedSourceId=theRemovedSource" );
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( RemoveSourceCommand.removedSourceId ) ).toBe( cmd.getArg( RemoveSourceCommand.removedSourceId ) );
});

it("RemoveSourceCommand Undo Test", () => {
const cmd = CommandFactory.createRemoveSourceCommand( "theRemovedSource", true );
expect( cmd.canUndo() ).toBe( true );
expect( cmd.undoCommand ).not.toBeNull();
expect( cmd.undoCommand.id ).toBe( AddSourceCommand.id );
expect( cmd.undoCommand.args.size ).toBe( 1 );
expect( cmd.undoCommand.getArg( AddSourceCommand.addedSourceId ) ).toEqual( cmd.getArg( RemoveSourceCommand.removedSourceId ) );
});

it("UpdateViewDescriptionCommand Test", () => {
const cmd = CommandFactory.createUpdateViewDescriptionCommand( "theNewDescription",
"theOldDescription" );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 2 );
expect( cmd.getArg( UpdateViewDescriptionCommand.newDescription ) ).toEqual( "theNewDescription" );
expect( cmd.getArg( UpdateViewDescriptionCommand.oldDescription ) ).toEqual( "theOldDescription" );
expect( cmd.toString() ).toEqual( "UpdateViewDescriptionCommand, newDescription=theNewDescription, oldDescription=theOldDescription" );
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( UpdateViewDescriptionCommand.newDescription ) )
.toBe( cmd.getArg( UpdateViewDescriptionCommand.newDescription ) );
expect( roundtrip.getArg( UpdateViewDescriptionCommand.oldDescription ) )
.toBe( cmd.getArg( UpdateViewDescriptionCommand.oldDescription ) );
});

it("UpdateViewDescriptionCommand Undo Test", () => {
const cmd = CommandFactory.createUpdateViewDescriptionCommand( "theNewDescription",
"theOldDescription",
true );
expect( cmd.canUndo() ).toBe( true );
expect( cmd.undoCommand ).not.toBeNull();
expect( cmd.undoCommand.id ).toBe( UpdateViewDescriptionCommand.id );
expect( cmd.undoCommand.args.size ).toBe( 2 );
expect( cmd.undoCommand.getArg( UpdateViewDescriptionCommand.newDescription ) )
.toBe( cmd.getArg( UpdateViewDescriptionCommand.oldDescription ) );
expect( cmd.undoCommand.getArg( UpdateViewDescriptionCommand.oldDescription ) )
.toBe( cmd.getArg( UpdateViewDescriptionCommand.newDescription ) );
});

it("UpdateViewNameCommand Test", () => {
const cmd = CommandFactory.createUpdateViewNameCommand( "theNewName", "theOldName" );
expect( cmd.args ).not.toBeNull();
expect( cmd.args.size ).toBe( 2 );
expect( cmd.getArg( UpdateViewNameCommand.newName ) ).toEqual( "theNewName" );
expect( cmd.getArg( UpdateViewNameCommand.oldName ) ).toEqual( "theOldName" );
expect( cmd.toString() ).toEqual( "UpdateViewNameCommand, newName=theNewName, oldName=theOldName" );
expect( cmd.canUndo() ).toBe( false );
expect( cmd.undoCommand ).toBeNull();

const json = cmd.toJSON();
const roundtrip = CommandFactory.decode( json );
expect( roundtrip.getArg( UpdateViewNameCommand.newName ) )
.toBe( cmd.getArg( UpdateViewNameCommand.newName ) );
expect( roundtrip.getArg( UpdateViewNameCommand.oldName ) )
.toBe( cmd.getArg( UpdateViewNameCommand.oldName ) );
});

it("UpdateViewNameCommand Undo Test", () => {
const cmd = CommandFactory.createUpdateViewNameCommand( "theNewName",
"theOldName",
true );
expect( cmd.canUndo() ).toBe( true );
expect( cmd.undoCommand ).not.toBeNull();
expect( cmd.undoCommand.id ).toBe( UpdateViewNameCommand.id );
expect( cmd.undoCommand.args.size ).toBe( 2 );
expect( cmd.undoCommand.getArg( UpdateViewNameCommand.newName ) )
.toBe( cmd.getArg( UpdateViewNameCommand.oldName ) );
expect( cmd.undoCommand.getArg( UpdateViewNameCommand.oldName ) )
.toBe( cmd.getArg( UpdateViewNameCommand.newName ) );
});

});
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/**
* @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 { Command } from "@dataservices/virtualization/view-editor/command/command";
import { AddSourceCommand } from "@dataservices/virtualization/view-editor/command/add-source-command";
import { RemoveSourceCommand } from "@dataservices/virtualization/view-editor/command/remove-source-command";
import { UpdateViewDescriptionCommand } from "@dataservices/virtualization/view-editor/command/update-view-description-command";
import { UpdateViewNameCommand } from "@dataservices/virtualization/view-editor/command/update-view-name-command";

export class CommandFactory {

/**
* @param {string} addedSourceId the ID of the source being added (cannot be `null` or empty)
* @param {boolean} createUndoCommand `true` if an undo command should be created
* @returns {Command} the undo command (never `null`)
*/
public static createAddSourceCommand( addedSourceId: string,
createUndoCommand: boolean = false ): Command {
const cmd = new AddSourceCommand( addedSourceId );

if ( createUndoCommand ) {
cmd.undoCommand = CommandFactory.createUndoCommand( cmd );
}

return cmd;
}

/**
* @param {string} removedSourceId the ID of the source being removed (cannot be `null` or empty)
* @param {boolean} createUndoCommand `true` if an undo command should be created
* @returns {Command} the undo command (never `null`)
*/
public static createRemoveSourceCommand( removedSourceId: string,
createUndoCommand: boolean = false ): Command {
const cmd = new RemoveSourceCommand( removedSourceId );

if ( createUndoCommand ) {
cmd.undoCommand = CommandFactory.createUndoCommand( cmd );
}

return cmd;
}

/**
* @param {Command} cmd the command whose undo command is being requested
* @returns {Command | null} the undo command or `null` if one cannot be created
*/
public static createUndoCommand( cmd: Command ): Command | null {
let undoCmd: Command = null;

switch ( cmd.id ) {
case AddSourceCommand.id: {
undoCmd = CommandFactory.createRemoveSourceCommand( cmd.getArg( AddSourceCommand.addedSourceId ) );
break;
}
case RemoveSourceCommand.id: {
undoCmd = CommandFactory.createAddSourceCommand( cmd.getArg( RemoveSourceCommand.removedSourceId ) );
break;
}
case UpdateViewDescriptionCommand.id: {
undoCmd = CommandFactory.createUpdateViewDescriptionCommand( cmd.getArg( UpdateViewDescriptionCommand.oldDescription ),
cmd.getArg( UpdateViewDescriptionCommand.newDescription ) );
break;
}
case UpdateViewNameCommand.id: {
undoCmd = CommandFactory.createUpdateViewNameCommand( cmd.getArg( UpdateViewNameCommand.oldName ),
cmd.getArg( UpdateViewNameCommand.newName ) );
break;
}
default: {
break;
}
}

return undoCmd;
}

/**
* @param {string} newViewDescription the new view description (can be `null` or empty)
* @param {string} replacedViewDescription the view description being replaced (can be `null` or empty)
* @param {boolean} createUndoCommand `true` if an undo command should be created
* @returns {Command} the undo command (never `null`)
*/
public static createUpdateViewDescriptionCommand( newViewDescription: string,
replacedViewDescription: string,
createUndoCommand: boolean = false ): Command {
const cmd = new UpdateViewDescriptionCommand( newViewDescription, replacedViewDescription );

if ( createUndoCommand ) {
cmd.undoCommand = CommandFactory.createUndoCommand( cmd );
}

return cmd;
}

/**
* @param {string} newViewName the new view name (can be `null` or empty)
* @param {string} replacedViewName the view name being replaced (can be `null` or empty)
* @param {boolean} createUndoCommand `true` if an undo command should be created
* @returns {Command}
*/
public static createUpdateViewNameCommand( newViewName: string,
replacedViewName: string,
createUndoCommand: boolean = false ): Command {
const cmd = new UpdateViewNameCommand( newViewName, replacedViewName );

if ( createUndoCommand ) {
cmd.undoCommand = CommandFactory.createUndoCommand( cmd );
}

return cmd;
}

/**
* Constructs a command object from JSON.
*
* @param {object} json the JSON being converted to a command
* @returns {Command | null} the command or `null` if one could not be constructed
*/
public static decode( json: object = {} ): Command | null {
const cmdId = json[ Command.idPropJson ];
let cmd: Command = null;

switch ( cmdId ) {
case AddSourceCommand.id: {
for ( const entry of json[ Command.argsPropJson ] ) {
if ( entry[ Command.argNameJson ] === AddSourceCommand.addedSourceId ) {
cmd = CommandFactory.createAddSourceCommand( entry[ Command.argValueJson ] );
break;
}
}

break;
}
case RemoveSourceCommand.id: {
for ( const entry of json[ Command.argsPropJson ] ) {
if ( entry[ Command.argNameJson ] === RemoveSourceCommand.removedSourceId ) {
cmd = CommandFactory.createRemoveSourceCommand( entry[ Command.argValueJson ] );
break;
}
}

break;
}
case UpdateViewDescriptionCommand.id: {
let newViewDescription: string = null;
let replacedViewDescription: string = null;

for ( const entry of json[ Command.argsPropJson ] ) {
if ( entry[ Command.argNameJson ] === UpdateViewDescriptionCommand.newDescription ) {
newViewDescription = entry[ Command.argValueJson ];
}
else if ( entry[ Command.argNameJson ] === UpdateViewDescriptionCommand.oldDescription ) {
replacedViewDescription = entry[ Command.argValueJson ];
}

if ( newViewDescription && replacedViewDescription ) {
break;
}
}

if ( newViewDescription && replacedViewDescription ) {
cmd = CommandFactory.createUpdateViewDescriptionCommand( newViewDescription, replacedViewDescription );
}

break;
}
case UpdateViewNameCommand.id: {
let newViewName: string = null;
let replacedViewName: string = null;

for ( const entry of json[ Command.argsPropJson ] ) {
if ( entry[ Command.argNameJson ] === UpdateViewNameCommand.newName ) {
newViewName = entry[ "value" ];
}
else if ( entry[ Command.argNameJson ] === UpdateViewNameCommand.oldName ) {
replacedViewName = entry[ "value" ];
}

if ( newViewName && replacedViewName ) {
break;
}
}

if ( newViewName && replacedViewName ) {
cmd = CommandFactory.createUpdateViewNameCommand( newViewName, replacedViewName );
}

break;
}
default: {
console.error( "Unable to create command from json: " + json );
break;
}
}

return cmd;
}

}

0 comments on commit 5aad1f4

Please sign in to comment.