Skip to content
Snippets Groups Projects
Commit 63422f95 authored by Marc Hanisch's avatar Marc Hanisch
Browse files

removes IPathSetter interface and implementations

parent da114539
No related branches found
No related tags found
1 merge request!33Feature configuration models
Showing
with 16 additions and 592 deletions
import IPathSetter from './IPathSetter'
export interface ICompartment {
id: string
name: string
uri: string
}
export default class Compartment implements ICompartment, IPathSetter {
export default class Compartment implements ICompartment {
private _id: string = ''
private _name: string = ''
private _uri: string = ''
......@@ -39,25 +37,6 @@ export default class Compartment implements ICompartment, IPathSetter {
return this._name
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
this.id = String(value)
break
case 'name':
this.name = String(value)
break
case 'uri':
this.uri = String(value)
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createWithData (id: string, name: string, uri: string): Compartment {
const result = new Compartment()
result.id = id
......
import IPathSetter from '@/models/IPathSetter'
import Contact, { IContact } from '@/models/Contact'
import { ConfigurationsTree } from '@/models/ConfigurationsTree'
import { ConfigurationsTreeNode } from '@/models/ConfigurationsTreeNode'
......@@ -17,7 +16,7 @@ export interface IConfiguration {
deviceAttributes: DeviceConfigurationAttributes[]
}
export class Configuration implements IConfiguration, IPathSetter {
export class Configuration implements IConfiguration {
private _id: number | null = null
private _startDate: Date | null = null
private _endDate: Date | null = null
......@@ -99,31 +98,6 @@ export class Configuration implements IConfiguration, IPathSetter {
this._deviceAttributes = attributes
}
setPath (path: string, value: any): void {
const paths = path.split('.')
const topLevelElement = paths.splice(0, 1)[0]
const tail = paths.join('.')
switch (topLevelElement) {
case 'id':
this.id = isNaN(parseInt(value)) ? null : parseInt(value)
break
case 'startDate':
this.startDate = value instanceof Date ? value : null
break
case 'endDate':
this.endDate = value instanceof Date ? value : null
break
case 'location':
// as the setPath method is implemented in the concrete classes, we have to cast the objects here
if (this.location instanceof StationaryLocation) {
(this.location as StationaryLocation).setPath(tail, value)
}
break
default:
throw new TypeError('path ' + path + ' is not defined')
}
}
static createFromObject (someObject: Configuration): Configuration {
const newObject = new Configuration()
......
import IPathSetter from './IPathSetter'
export interface IContact {
id: number | null
email: string
......@@ -8,7 +6,7 @@ export interface IContact {
website: string
}
export default class Contact implements IContact, IPathSetter {
export default class Contact implements IContact {
private _id: number | null = null
private _email: string = ''
private _givenName: string = ''
......@@ -65,35 +63,6 @@ export default class Contact implements IContact, IPathSetter {
return 'Contact ' + this._id
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
if (value !== null) {
this.id = Number(value)
} else {
this.id = null
}
break
case 'email':
this.email = String(value)
break
case 'givenName':
this.givenName = String(value)
break
case 'familyName':
this.familyName = String(value)
break
case 'website':
this.website = String(value)
break
default:
throw new TypeError('path ' + path + ' is not valid')
}
}
static createWithIdEMailAndNames (id: number, email: string, givenName: string, familyName: string, website: string): Contact {
const result = new Contact()
result.id = id
......
......@@ -3,7 +3,6 @@ import { DeviceProperty } from './DeviceProperty'
import { CustomTextField, ICustomTextField } from './CustomTextField'
import { Attachment, IAttachment } from './Attachment'
import IPathSetter from './IPathSetter'
export interface IDevice {
id: number | null
......@@ -38,7 +37,7 @@ export interface IDevice {
attachments: IAttachment[]
}
export default class Device implements IDevice, IPathSetter {
export default class Device implements IDevice {
private _id: number | null = null
private _persistentIdentifier: string = ''
private _shortName: string = ''
......@@ -266,108 +265,6 @@ export default class Device implements IDevice, IPathSetter {
this._attachments = attachments
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
if (value !== null) {
this.id = Number(value)
} else {
this.id = null
}
break
case 'persistentIdentifier':
this.persistentIdentifier = String(value)
break
case 'shortName':
this.shortName = String(value)
break
case 'longName':
this.longName = String(value)
break
case 'statusUri':
this.statusUri = String(value)
break
case 'statusName':
this.statusName = String(value)
break
case 'manufacturerUri':
this.manufacturerUri = String(value)
break
case 'manufacturerName':
this.manufacturerName = String(value)
break
case 'deviceTypeUri':
this.deviceTypeUri = String(value)
break
case 'deviceTypeName':
this.deviceTypeName = String(value)
break
case 'model':
this.model = String(value)
break
case 'description':
this.description = String(value)
break
case 'website':
this.website = String(value)
break
case 'serialNumber':
this.serialNumber = String(value)
break
case 'inventoryNumber':
this.inventoryNumber = String(value)
break
case 'dualUse':
this.dualUse = Boolean(value)
break
case 'createdAt':
if (value !== null) {
this.createdAt = value as Date
} else {
this.createdAt = null
}
break
case 'modifiedAt':
if (value !== null) {
this.modifiedAt = value as Date
} else {
this.modifiedAt = null
}
break
case 'createdByUserId':
if (value !== null) {
this.createdByUserId = Number(value)
} else {
this.createdByUserId = null
}
break
case 'modifiedByUserId':
if (value !== null) {
this.modifiedByUserId = Number(value)
} else {
this.modifiedByUserId = null
}
break
case 'contacts':
this.contacts = value.map(Contact.createFromObject)
break
case 'properties':
this.properties = value.map(DeviceProperty.createFromObject)
break
case 'customFields':
this.customFields = value.map(CustomTextField.createFromObject)
break
case 'attachments':
this.attachments = value.map(Attachment.createFromObject)
break
default:
throw new TypeError('path ' + path + ' is not valid')
}
}
static createFromObject (someObject: IDevice): Device {
const newObject = new Device()
......
import { DeviceProperty } from '@/models/DeviceProperty'
import Device from '@/models/Device'
import IPathSetter from '@/models/IPathSetter'
export interface IDeviceConfigurationAttributes {
device: Device
......@@ -11,7 +10,7 @@ export interface IDeviceConfigurationAttributes {
deviceProperties: DeviceProperty[]
}
export class DeviceConfigurationAttributes implements IDeviceConfigurationAttributes, IPathSetter {
export class DeviceConfigurationAttributes implements IDeviceConfigurationAttributes {
private _device: Device
private _offsetX: number = 0
private _offsetY: number = 0
......@@ -35,25 +34,6 @@ export class DeviceConfigurationAttributes implements IDeviceConfigurationAttrib
return newObject
}
setPath (path: string, value: any): void {
switch (path) {
case 'offsetX':
this.offsetX = isNaN(value) ? 0 : parseInt(value)
break
case 'offsetY':
this.offsetY = isNaN(value) ? 0 : parseInt(value)
break
case 'offsetZ':
this.offsetZ = isNaN(value) ? 0 : parseInt(value)
break
case 'calibrationDate':
this.calibrationDate = value instanceof Date ? value : null
break
default:
throw new TypeError('path ' + path + ' is not defined')
}
}
get id (): number | null {
return this._device.id
}
......
import IPathSetter from './IPathSetter'
export interface IDeviceType {
id: string
name: string
uri: string
}
export default class DeviceType implements IDeviceType, IPathSetter {
export default class DeviceType implements IDeviceType {
private _id: string = ''
private _name: string = ''
private _uri: string = ''
......@@ -39,25 +37,6 @@ export default class DeviceType implements IDeviceType, IPathSetter {
return this._name
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
this.id = String(value)
break
case 'name':
this.name = String(value)
break
case 'uri':
this.uri = String(value)
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createWithData (id: string, name: string, uri: string): DeviceType {
const result = new DeviceType()
result.id = id
......
export default interface IPathSetter {
setPath (path: string, value: any): void
}
import IPathSetter from '@/models/IPathSetter'
import { DeviceProperty } from '@/models/DeviceProperty'
export interface IStationaryLocation {
......@@ -8,7 +7,7 @@ export interface IStationaryLocation {
elevation: number | null
}
export class StationaryLocation implements IStationaryLocation, IPathSetter {
export class StationaryLocation implements IStationaryLocation {
private _latitude: number | null = null
private _longitude: number | null = null
private _elevation: number | null = null
......@@ -41,24 +40,6 @@ export class StationaryLocation implements IStationaryLocation, IPathSetter {
this._elevation = elevation
}
setPath (path: string, value: any): void {
switch (path) {
case 'type':
throw new TypeError('path type is readonly')
case 'latitude':
this.latitude = isNaN(parseFloat(value)) ? 0 : parseFloat(value)
break
case 'longitude':
this.longitude = isNaN(parseFloat(value)) ? 0 : parseFloat(value)
break
case 'elevation':
this.elevation = isNaN(parseFloat(value)) ? 0 : parseFloat(value)
break
default:
throw new TypeError('path ' + path + ' is not defined')
}
}
static createFromObject (someObject: IStationaryLocation): StationaryLocation {
const newObject: StationaryLocation = new StationaryLocation()
newObject.latitude = someObject.latitude
......
import IPathSetter from './IPathSetter'
export interface IManufacturer {
id: string
name: string
uri: string
}
export default class Manufacturer implements IManufacturer, IPathSetter {
export default class Manufacturer implements IManufacturer {
private _id: string = ''
private _name: string = ''
private _uri: string = ''
......@@ -39,25 +37,6 @@ export default class Manufacturer implements IManufacturer, IPathSetter {
return this._name
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
this.id = String(value)
break
case 'name':
this.name = String(value)
break
case 'uri':
this.uri = String(value)
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createWithData (id: string, name: string, uri: string): Manufacturer {
const result = new Manufacturer()
result.id = id
......
import Contact, { IContact } from './Contact'
import { Attachment, IAttachment } from './Attachment'
import IPathSetter from './IPathSetter'
export interface IPlatform {
id: number | null
......@@ -36,7 +34,7 @@ export interface IPlatform {
attachments: IAttachment[]
}
export default class Platform implements IPlatform, IPathSetter {
export default class Platform implements IPlatform {
private _id: number | null = null
private _platformTypeUri: string = ''
......@@ -235,99 +233,6 @@ export default class Platform implements IPlatform, IPathSetter {
this._attachments = attachments
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
if (value !== null) {
this.id = Number(value)
} else {
this.id = null
}
break
case 'platformTypeUri':
this.platformTypeUri = String(value)
break
case 'platformTypeName':
this.platformTypeName = String(value)
break
case 'shortName':
this.shortName = String(value)
break
case 'longName':
this.longName = String(value)
break
case 'description':
this.description = String(value)
break
case 'manufacturerUri':
this.manufacturerUri = String(value)
break
case 'manufacturerName':
this.manufacturerName = String(value)
break
case 'model':
this.model = String(value)
break
case 'statusUri':
this.statusUri = String(value)
break
case 'statusName':
this.statusName = String(value)
break
case 'inventoryNumber':
this.inventoryNumber = String(value)
break
case 'serialNumber':
this.serialNumber = String(value)
break
case 'website':
this.website = String(value)
break
case 'persistentIdentifier':
this.persistentIdentifier = String(value)
break
case 'createdAt':
if (value !== null) {
this.createdAt = value as Date
} else {
this.createdAt = null
}
break
case 'modifiedAt':
if (value !== null) {
this.modifiedAt = value as Date
} else {
this.modifiedAt = null
}
break
case 'createdByUserId':
if (value !== null) {
this.createdByUserId = Number(value)
} else {
this.createdByUserId = null
}
break
case 'modifiedByUserId':
if (value !== null) {
this.modifiedByUserId = Number(value)
} else {
this.modifiedByUserId = null
}
break
case 'contacts':
this.contacts = value.map(Contact.createFromObject)
break
case 'attachments':
this.attachments = value.map(Attachment.createFromObject)
break
default:
throw new TypeError('path ' + path + ' is not valid')
}
}
static createEmpty (): Platform {
return new Platform()
}
......
import Platform from '@/models/Platform'
import IPathSetter from '@/models/IPathSetter'
export interface IPlatformConfigurationAttributes {
platform: Platform
......@@ -8,7 +7,7 @@ export interface IPlatformConfigurationAttributes {
offsetZ: number
}
export class PlatformConfigurationAttributes implements IPlatformConfigurationAttributes, IPathSetter {
export class PlatformConfigurationAttributes implements IPlatformConfigurationAttributes {
private _platform: Platform
private _offsetX: number = 0
private _offsetY: number = 0
......@@ -28,22 +27,6 @@ export class PlatformConfigurationAttributes implements IPlatformConfigurationAt
return newObject
}
setPath (path: string, value: any): void {
switch (path) {
case 'offsetX':
this.offsetX = isNaN(value) ? 0 : parseInt(value)
break
case 'offsetY':
this.offsetY = isNaN(value) ? 0 : parseInt(value)
break
case 'offsetZ':
this.offsetZ = isNaN(value) ? 0 : parseInt(value)
break
default:
throw new TypeError('path ' + path + ' is not defined')
}
}
get id (): number | null {
return this._platform.id
}
......
import IPathSetter from './IPathSetter'
export interface IPlatformType {
id: string
name: string
uri: string
}
export default class PlatformType implements IPlatformType, IPathSetter {
export default class PlatformType implements IPlatformType {
private _id: string = ''
private _name: string = ''
private _uri: string = ''
......@@ -39,25 +37,6 @@ export default class PlatformType implements IPlatformType, IPathSetter {
return this._name
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
this.id = String(value)
break
case 'name':
this.name = String(value)
break
case 'uri':
this.uri = String(value)
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createWithData (id: string, name: string, uri: string): PlatformType {
const result = new PlatformType()
result.id = id
......
import IPathSetter from './IPathSetter'
export interface IVariable {
id: string
name: string
uri: string
}
export default class Variable implements IVariable, IPathSetter {
export default class Variable implements IVariable {
private _id: string = ''
private _name: string = ''
private _uri: string = ''
......@@ -39,25 +37,6 @@ export default class Variable implements IVariable, IPathSetter {
return this._name
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
this.id = String(value)
break
case 'name':
this.name = String(value)
break
case 'uri':
this.uri = String(value)
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createWithData (id: string, name: string, uri: string): Variable {
const result = new Variable()
result.id = id
......
import IPathSetter from './IPathSetter'
export interface ISamplingMedia {
id: string
name: string
uri: string
}
export default class SamplingMedia implements ISamplingMedia, IPathSetter {
export default class SamplingMedia implements ISamplingMedia {
private _id: string = ''
private _name: string = ''
private _uri: string = ''
......@@ -39,25 +37,6 @@ export default class SamplingMedia implements ISamplingMedia, IPathSetter {
return this._name
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
this.id = String(value)
break
case 'name':
this.name = String(value)
break
case 'uri':
this.uri = String(value)
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createWithData (id: string, name: string, uri: string): SamplingMedia {
const result = new SamplingMedia()
result.id = id
......
import IPathSetter from './IPathSetter'
export interface IStatus {
id: string
name: string
uri: string
}
export default class Status implements IStatus, IPathSetter {
export default class Status implements IStatus {
private _id: string = ''
private _name: string = ''
private _uri: string = ''
......@@ -39,25 +37,6 @@ export default class Status implements IStatus, IPathSetter {
return this._name
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
this.id = String(value)
break
case 'name':
this.name = String(value)
break
case 'uri':
this.uri = String(value)
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createWithData (id: string, name: string, uri: string): Status {
const result = new Status()
result.id = id
......
import IPathSetter from './IPathSetter'
export interface IUnit {
id: string
name: string
uri: string
}
export default class Unit implements IUnit, IPathSetter {
export default class Unit implements IUnit {
private _id: string = ''
private _name: string = ''
private _uri: string = ''
......@@ -39,25 +37,6 @@ export default class Unit implements IUnit, IPathSetter {
return this._name
}
setPath (path: string, value: any): void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
this.id = String(value)
break
case 'name':
this.name = String(value)
break
case 'uri':
this.uri = String(value)
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createWithData (id: string, name: string, uri: string): Unit {
const result = new Unit()
result.id = id
......
import Contact, { IContact } from './Contact'
import IPathSetter from './IPathSetter'
export interface IUser {
id: number | null
......@@ -7,7 +6,7 @@ export interface IUser {
contact: IContact | null
}
export default class User implements IUser, IPathSetter {
export default class User implements IUser {
private _id: number | null = null
private _subject: string = ''
private _contact: Contact | null = null
......@@ -36,33 +35,6 @@ export default class User implements IUser, IPathSetter {
this._contact = newContact
}
setPath (path: string, value: any) : void {
const pathArray = path.split('.')
const topLevelElement = pathArray.splice(0, 1)[0]
switch (topLevelElement) {
case 'id':
if (value !== null) {
this.id = Number(value)
} else {
this.id = null
}
break
case 'subject':
this.subject = String(value)
break
case 'contact':
if (value !== null) {
this.contact = Contact.createFromObject(value)
} else {
this.contact = null
}
break
default:
throw new TypeError('path ' + path + ' is not value')
}
}
static createFromObjet (someObject: IUser): User {
const newObject = new User()
......
......@@ -71,24 +71,6 @@ describe('DeviceConfigurationAttributes', () => {
expect(() => { attributes.addDevicePropertyById(prop1.id) }).toThrow(Error)
})
it('should set a property by its path', () => {
const device = new Device()
device.id = 1
const now = new Date()
const attributes = new DeviceConfigurationAttributes(device)
attributes.setPath('offsetX', 1)
attributes.setPath('offsetY', 1)
attributes.setPath('offsetZ', 1)
attributes.setPath('calibrationDate', now)
expect(attributes).toHaveProperty('id', 1)
expect(attributes).toHaveProperty('offsetX', 1)
expect(attributes).toHaveProperty('offsetY', 1)
expect(attributes).toHaveProperty('offsetZ', 1)
expect(attributes).toHaveProperty('calibrationDate', now)
})
it('should remove a deviceProperty', () => {
const prop1 = new DeviceProperty()
const device = new Device()
......@@ -122,12 +104,4 @@ describe('DeviceConfigurationAttributes', () => {
const attributes = new DeviceConfigurationAttributes(device)
expect(() => { attributes.addDevicePropertyById(prop1.id) }).toThrow(Error)
})
it('should throw an error when using a invalid path', () => {
const device = new Device()
device.id = 1
const attributes = new DeviceConfigurationAttributes(device)
expect(() => attributes.setPath('id', 2)).toThrow(TypeError)
})
})
......@@ -10,23 +10,6 @@ describe('StationaryLocation', () => {
expect(location).toHaveProperty('longitude', 12.973538)
expect(location).toHaveProperty('elevation', 280)
})
it('should set a property by its path', () => {
const location = new StationaryLocation()
location.setPath('latitude', 50.986451)
location.setPath('longitude', 12.973538)
location.setPath('elevation', 280)
expect(location).toHaveProperty('type', 'stationary')
expect(location).toHaveProperty('latitude', 50.986451)
expect(location).toHaveProperty('longitude', 12.973538)
expect(location).toHaveProperty('elevation', 280)
})
it('should throw an error when using a invalid path', () => {
const location = new StationaryLocation()
expect(() => location.setPath('foo', 'bar')).toThrow(TypeError)
})
})
describe('DynamicLocation', () => {
......
......@@ -23,27 +23,4 @@ describe('PlatformConfigurationAttributes', () => {
expect(attributes).toHaveProperty('offsetY', 1)
expect(attributes).toHaveProperty('offsetZ', 1)
})
it('should set a property by its path', () => {
const platform = new Platform()
platform.id = 1
const attributes = new PlatformConfigurationAttributes(platform)
attributes.setPath('offsetX', 1)
attributes.setPath('offsetY', 1)
attributes.setPath('offsetZ', 1)
expect(attributes).toHaveProperty('id', 1)
expect(attributes).toHaveProperty('offsetX', 1)
expect(attributes).toHaveProperty('offsetY', 1)
expect(attributes).toHaveProperty('offsetZ', 1)
})
it('should throw an error when using a invalid path', () => {
const platform = new Platform()
platform.id = 1
const attributes = new PlatformConfigurationAttributes(platform)
expect(() => attributes.setPath('id', 2)).toThrow(TypeError)
})
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment