Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VUU-27 remotePersistenceManager implementation #74

Merged
merged 18 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .semgrepignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ vuu/src/main/resources/www/ws-example.html
vuu/src/main/scala/org/finos/vuu/provider/simulation/SimulatedBigInstrumentsProvider.scala
vuu-ui/packages/vuu-data/src/array-data-source/group-utils.ts
vuu-ui/packages/vuu-datagrid-extras/src/column-expression-input/column-language-parser/walkExpressionTree.ts
vuu-ui/packages/vuu-layout/src/layout-persistence/RemoteLayoutPersistenceManager.ts
vuu-ui/packages/vuu-popups/src/menu/useContextMenu.tsx
vuu-ui/packages/vuu-table-extras/src/cell-edit-validators/PatternValidator.ts
vuu-ui/packages/vuu-ui-controls/src/list/Highlighter.tsx
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.finos.vuu.layoutserver;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://127.0.0.1:5173")
.allowedMethods("GET", "POST", "PUT", "DELETE");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface LayoutPersistenceManager {
*
* @returns Unique identifier assigned to the saved layout
*/
createLayout: (metadata: Omit<LayoutMetadata, "id">, layout: LayoutJSON) => Promise<string>;
createLayout: (metadata: Omit<LayoutMetadata, "id" | "created">, layout: LayoutJSON) => Promise<LayoutMetadata>;

/**
* Overwrites an existing layout and its corresponding metadata with the provided information
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
import { Layout, LayoutMetadata, WithId } from "@finos/vuu-shell";
import { LayoutJSON, LayoutPersistenceManager } from "@finos/vuu-layout";
import { getLocalEntity, saveLocalEntity } from "@finos/vuu-filters";
import { getUniqueId } from "@finos/vuu-utils";
import { formatDate, getUniqueId } from "@finos/vuu-utils";

import { defaultLayout } from "./data";
import { LayoutPersistenceManager } from "./LayoutPersistenceManager";
import { LayoutJSON } from "../layout-reducer";

const metadataSaveLocation = "layouts/metadata";
const layoutsSaveLocation = "layouts/layouts";

export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
createLayout(metadata: Omit<LayoutMetadata, "id">, layout: LayoutJSON): Promise<string> {
return new Promise(resolve => {
console.log(`Saving layout as ${metadata.name} to group ${metadata.group}...`);
createLayout(
metadata: Omit<LayoutMetadata, "id" | "created">,
layout: LayoutJSON
): Promise<LayoutMetadata> {
return new Promise((resolve) => {
console.log(
`Saving layout as ${metadata.name} to group ${metadata.group}...`
);

Promise.all([this.loadLayouts(), this.loadMetadata()])
.then(([existingLayouts, existingMetadata]) => {
Promise.all([this.loadLayouts(), this.loadMetadata()]).then(
([existingLayouts, existingMetadata]) => {
const id = getUniqueId();
const newMetadata = {
...metadata,
id,
created: formatDate(new Date(), "dd.mm.yyyy"),
};
this.appendAndPersist(
id,
metadata,
newMetadata,
layout,
existingLayouts,
existingMetadata
);
resolve(id);
});
})
resolve(newMetadata);
}
);
});
}

updateLayout(
Expand All @@ -37,12 +49,14 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
this.validateIds(id)
.then(() => Promise.all([this.loadLayouts(), this.loadMetadata()]))
.then(([existingLayouts, existingMetadata]) => {
const layouts = existingLayouts.filter(layout => layout.id !== id);
const metadata = existingMetadata.filter(metadata => metadata.id !== id);
const layouts = existingLayouts.filter((layout) => layout.id !== id);
const metadata = existingMetadata.filter(
(metadata) => metadata.id !== id
);
this.appendAndPersist(id, newMetadata, newLayout, layouts, metadata);
resolve();
})
.catch(e => reject(e));
.catch((e) => reject(e));
});
}

Expand All @@ -51,24 +65,32 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
this.validateIds(id)
.then(() => Promise.all([this.loadLayouts(), this.loadMetadata()]))
.then(([existingLayouts, existingMetadata]) => {
const layouts = existingLayouts.filter(layout => layout.id !== id);
const metadata = existingMetadata.filter(metadata => metadata.id !== id);
const layouts = existingLayouts.filter((layout) => layout.id !== id);
const metadata = existingMetadata.filter(
(metadata) => metadata.id !== id
);
this.saveLayoutsWithMetadata(layouts, metadata);
resolve();
})
.catch(e => reject(e));
.catch((e) => reject(e));
});
}

loadLayout(id: string): Promise<LayoutJSON> {
return new Promise((resolve, reject) => {
this.validateId(id, "layout")
.then(() => this.loadLayouts())
.then(existingLayouts => {
const layouts = existingLayouts.find(layout => layout.id === id) as Layout;
resolve(layouts.json);
.then((existingLayouts) => {
const foundLayout = existingLayouts.find(
(layout) => layout.id === id
);
if (foundLayout) {
resolve(foundLayout.json);
} else {
reject(new Error(`no layout found matching id ${id}`));
}
})
.catch(e => reject(e));
.catch((e) => reject(e));
});
}

Expand Down Expand Up @@ -102,7 +124,7 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
}

private loadLayouts(): Promise<Layout[]> {
return new Promise(resolve => {
return new Promise((resolve) => {
const layouts = getLocalEntity<Layout[]>(layoutsSaveLocation);
resolve(layouts || []);
});
Expand Down Expand Up @@ -132,29 +154,33 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
// Ensures that there is exactly one Layout entry and exactly one Metadata
// entry in local storage corresponding to the provided ID.
private async validateIds(id: string): Promise<void> {
return Promise
.all([
this.validateId(id, "metadata").catch(error => error.message),
this.validateId(id, "layout").catch(error => error.message)
])
.then((errorMessages: string[]) => {
// filter() is used to remove any blank messages before joining.
// Avoids orphaned delimiters in combined messages, e.g. "; " or "; error 2"
const combinedMessage = errorMessages.filter(msg => msg !== undefined).join("; ");
if (combinedMessage) {
throw new Error(combinedMessage);
}
});
return Promise.all([
this.validateId(id, "metadata").catch((error) => error.message),
this.validateId(id, "layout").catch((error) => error.message),
]).then((errorMessages: string[]) => {
// filter() is used to remove any blank messages before joining.
// Avoids orphaned delimiters in combined messages, e.g. "; " or "; error 2"
const combinedMessage = errorMessages
.filter((msg) => msg !== undefined)
.join("; ");
if (combinedMessage) {
throw new Error(combinedMessage);
}
});
}

// Ensures that there is exactly one element (Layout or Metadata) in local
// storage corresponding to the provided ID.
private validateId(id: string, dataType: "metadata" | "layout"): Promise<void> {
private validateId(
id: string,
dataType: "metadata" | "layout"
): Promise<void> {
return new Promise((resolve, reject) => {
const loadFunc = dataType === "metadata" ? this.loadMetadata : this.loadLayouts;
const loadFunc =
dataType === "metadata" ? this.loadMetadata : this.loadLayouts;

loadFunc().then((array: WithId[]) => {
const count = array.filter(element => element.id === id).length;
const count = array.filter((element) => element.id === id).length;
switch (count) {
case 1: {
resolve();
Expand All @@ -164,9 +190,10 @@ export class LocalLayoutPersistenceManager implements LayoutPersistenceManager {
reject(new Error(`No ${dataType} with ID ${id}`));
break;
}
default: reject(new Error(`Non-unique ${dataType} with ID ${id}`));
default:
reject(new Error(`Non-unique ${dataType} with ID ${id}`));
}
});
})
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { LayoutMetadata } from "@finos/vuu-shell";
import { LayoutPersistenceManager } from "./LayoutPersistenceManager";
import { LayoutJSON } from "../layout-reducer";
import { defaultLayout } from "./data";

const baseURL = "http://127.0.0.1:8081/api";
const metadataSaveLocation = "layouts/metadata";
const layoutsSaveLocation = "layouts";

export class RemoteLayoutPersistenceManager
implements LayoutPersistenceManager
{
createLayout(
metadata: Omit<LayoutMetadata, "id" | "created">,
layout: LayoutJSON
): Promise<LayoutMetadata> {
return new Promise((resolve, reject) =>
fetch(`${baseURL}/${layoutsSaveLocation}`, {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: JSON.stringify({
metadata: { ...metadata },
definition: JSON.stringify(layout),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do need this JSON.stringify() until we change the definition type #81

}),
})
.then((response) => {
if (!response.ok) {
reject(new Error(response.statusText));
}
response.json().then(({ metadata }: { metadata: LayoutMetadata }) => {
if (!metadata) {
reject(new Error("invalid metadata"));
}
resolve(metadata);
});
})
.catch((error: Error) => {
reject(error);
})
);
}

updateLayout(
id: string,
metadata: Omit<LayoutMetadata, "id">,
newLayoutJson: LayoutJSON
): Promise<void> {
return new Promise((resolve, reject) =>
fetch(`${baseURL}/${layoutsSaveLocation}/${id}`, {
method: "PUT",
body: JSON.stringify({
metadata,
layout: newLayoutJson,
}),
})
.then((response) => {
if (!response.ok) {
reject(new Error(response.statusText));
}
resolve();
})
.catch((error: Error) => {
reject(error);
})
);
}

deleteLayout(id: string): Promise<void> {
return new Promise((resolve, reject) =>
fetch(`${baseURL}/${layoutsSaveLocation}/${id}`, {
method: "DELETE",
})
.then((response) => {
if (!response.ok) {
reject(new Error(response.statusText));
}
resolve();
})
.catch((error: Error) => {
reject(error);
})
);
}

loadLayout(id: string): Promise<LayoutJSON> {
return new Promise((resolve, reject) => {
fetch(`${baseURL}/${layoutsSaveLocation}/${id}`, {})
.then((response) => {
if (!response.ok) {
reject(new Error(response.statusText));
}
response.json().then((layout) => {
if (!layout) {
reject(new Error("invalid layout"));
}
resolve(layout);
});
})
.catch((error: Error) => {
reject(error);
});
});
}

loadMetadata(): Promise<LayoutMetadata[]> {
return new Promise((resolve, reject) =>
fetch(`${baseURL}/${metadataSaveLocation}`, {})
.then(async (response) => {
if (!response.ok) {
reject(new Error(response.statusText));
}
response.json().then((metadata: LayoutMetadata[]) => {
if (!metadata) {
reject(new Error("invalid metadata"));
}
resolve(metadata);
});
})
.catch((error: Error) => {
reject(error);
})
);
}

saveApplicationLayout(layout: LayoutJSON): Promise<void> {
// TODO POST api/layouts/application #71
console.log(layout);
return new Promise((resolve) => resolve());
}

loadApplicationLayout(): Promise<LayoutJSON> {
// TODO GET api/layouts/application #71
return new Promise((resolve) => resolve(defaultLayout));
}
}
1 change: 1 addition & 0 deletions vuu-ui/packages/vuu-layout/src/layout-persistence/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './LayoutPersistenceManager';
export * from './LocalLayoutPersistenceManager';
export * from './RemoteLayoutPersistenceManager';
export * from './data';
Loading