Skip to content

Commit

Permalink
Merge pull request #2178 from robertbasti/main
Browse files Browse the repository at this point in the history
Copy with range to server #2051
  • Loading branch information
robertbasti authored Jan 23, 2024
2 parents d2edf16 + 42577b8 commit 1168469
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 195 deletions.
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Models/JobType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public enum JobType
AnalyzeGaps,
SpliceLogs,
CompareLogData,
CountLogDataRows,
CountLogDataRows
}
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,98 @@
import { Typography } from "@equinor/eds-core-react";
import { MenuItem } from "@material-ui/core";
import {
menuItemText,
pluralize
} from "components/ContextMenus/ContextMenuUtils";
import { menuItemText } from "components/ContextMenus/ContextMenuUtils";
import { useContext } from "react";
import NestedMenuItem from "components/ContextMenus/NestedMenuItem";
import { displayMissingObjectModal } from "components/Modals/MissingObjectModals";
import { displayReplaceModal } from "components/Modals/ReplaceModal";
import NavigationContext from "contexts/navigationContext";
import OperationContext from "contexts/operationContext";
import { DispatchOperation } from "contexts/operationStateReducer";
import OperationType from "contexts/operationType";
import { ComponentType, getParentType } from "models/componentType";
import ComponentReferences, {
createComponentReferences
} from "models/jobs/componentReferences";
import { CopyComponentsJob } from "models/jobs/copyJobs";
import { DeleteComponentsJob } from "models/jobs/deleteJobs";
import ObjectReference from "models/jobs/objectReference";
import { ReplaceComponentsJob } from "models/jobs/replaceComponentsJob";
import LogCurveInfo from "models/logCurveInfo";
import ObjectOnWellbore, { toObjectReference } from "models/objectOnWellbore";
import { ComponentType } from "models/componentType";
import { Server } from "models/server";
import { Fragment, useContext } from "react";
import AuthorizationService from "services/authorizationService";
import ComponentService from "services/componentService";
import JobService, { JobType } from "services/jobService";
import ObjectService from "services/objectService";
import { copyComponentsToServer } from "./CopyComponentsToServerUtils";
import CopyRangeModal, {
CopyRangeModalProps
} from "components/Modals/CopyRangeModal";

export interface CopyComponentsToServerMenuItemProps {
componentsToCopy: { uid: string }[];
componentType: ComponentType;
withRange?: boolean;
}

interface ComponentWithRange {
uid: string;
minIndex: number | Date;
maxIndex: number | Date;
}

export const CopyComponentsToServerMenuItem = (
props: CopyComponentsToServerMenuItemProps
): React.ReactElement => {
const { componentsToCopy, componentType } = props;
const { componentsToCopy, componentType, withRange } = props;
const {
navigationState: { selectedServer, selectedObject, servers }
} = useContext(NavigationContext);
const { dispatchOperation } = useContext(OperationContext);
const menuComponents = menuItemText("copy", componentType, componentsToCopy);
const menuText =
withRange === true
? menuComponents + " with range to server"
: menuComponents + " to server";

const onClickHandler = (server: Server) => {
if (withRange === true) {
const copyRangeModalProps: CopyRangeModalProps = {
mnemonics: [],
infoMessage:
"This will replace all data in your selected range on the target with data from the source. Data outside this range will be preserved on target.",
onSubmit(startIndex, endIndex) {
const componentsToCopyWithRange =
componentsToCopy as ComponentWithRange[];
const componentsRange = componentsToCopyWithRange.filter(
(x) => x.minIndex <= endIndex && x.maxIndex >= startIndex
);
copyComponentsToServer({
targetServer: server,
sourceServer: selectedServer,
componentsToCopy: componentsRange,
dispatchOperation,
sourceParent: selectedObject,
componentType: componentType,
startIndex: startIndex.toString(),
endIndex: endIndex.toString()
});
}
};
dispatchOperation({
type: OperationType.DisplayModal,
payload: <CopyRangeModal {...copyRangeModalProps} />
});
} else {
copyComponentsToServer({
targetServer: server,
sourceServer: selectedServer,
componentsToCopy,
dispatchOperation,
sourceParent: selectedObject,
componentType
});
}
};

return (
<NestedMenuItem
key={"copyComponentToServer"}
label={`${menuItemText(
"copy",
componentType,
componentsToCopy
)} to server`}
label={menuText}
disabled={componentsToCopy.length < 1}
>
{servers.map(
(server: Server) =>
server.id !== selectedServer.id && (
<MenuItem
key={server.name}
onClick={() =>
copyComponentsToServer({
targetServer: server,
sourceServer: selectedServer,
componentsToCopy,
dispatchOperation,
sourceParent: selectedObject,
componentType
})
}
onClick={() => {
onClickHandler(server);
}}
disabled={componentsToCopy.length < 1}
>
<Typography color={"primary"}>{server.name}</Typography>
Expand All @@ -76,145 +102,3 @@ export const CopyComponentsToServerMenuItem = (
</NestedMenuItem>
);
};

export interface OnClickCopyComponentToServerProps {
targetServer: Server;
sourceServer: Server;
componentsToCopy: { uid: string }[] | LogCurveInfo[];
dispatchOperation: DispatchOperation;
sourceParent: ObjectOnWellbore;
componentType: ComponentType;
}

const copyComponentsToServer = async (
props: OnClickCopyComponentToServerProps
) => {
const {
targetServer,
sourceServer,
componentsToCopy,
dispatchOperation,
sourceParent,
componentType
} = props;
dispatchOperation({ type: OperationType.HideContextMenu });
const wellUid = sourceParent.wellUid;
const wellboreUid = sourceParent.wellboreUid;
const parentUid = sourceParent.uid;
const parentType = getParentType(componentType);
const getId =
componentType == ComponentType.Mnemonic
? (component: any) => {
return component.mnemonic;
}
: (component: any) => {
return component.uid;
};

const targetParent = await ObjectService.getObjectFromServer(
wellUid,
wellboreUid,
parentUid,
parentType,
targetServer
);
if (targetParent?.uid !== parentUid) {
displayMissingObjectModal(
targetServer,
wellUid,
wellboreUid,
parentUid,
dispatchOperation,
`No ${pluralize(componentType)} will be copied.`,
parentType
);
return;
}
const sourceComponentReferences: ComponentReferences =
createComponentReferences(
componentsToCopy.map((component) => getId(component)),
sourceParent,
componentType,
sourceServer.url
);
const targetParentReference: ObjectReference =
toObjectReference(targetParent);
const copyJob: CopyComponentsJob = {
source: sourceComponentReferences,
target: targetParentReference
};

const allTargetComponents = await ComponentService.getComponents(
wellUid,
wellboreUid,
parentUid,
componentType,
targetServer
);
const existingTargetComponents = allTargetComponents.filter((component) =>
componentsToCopy.find(
(componentToCopy) => getId(componentToCopy) === getId(component)
)
);
if (existingTargetComponents.length > 0) {
const onConfirm = async () => {
dispatchOperation({ type: OperationType.HideModal });
const deleteJob: DeleteComponentsJob = {
toDelete: createComponentReferences(
existingTargetComponents.map((component) => getId(component)),
targetParent,
componentType
)
};
const replaceJob: ReplaceComponentsJob = { deleteJob, copyJob };
await JobService.orderJobAtServer(
JobType.ReplaceComponents,
replaceJob,
targetServer,
sourceServer
);
};
const print =
componentType == ComponentType.Mnemonic ? printCurveInfo : printUid;
displayReplaceModal(
existingTargetComponents,
componentsToCopy,
componentType,
parentType,
dispatchOperation,
onConfirm,
print
);
} else {
AuthorizationService.setSourceServer(sourceServer);
JobService.orderJobAtServer(
JobType.CopyComponents,
copyJob,
targetServer,
sourceServer
);
}
};

function printUid(component: { uid: string }): JSX.Element {
return (
<Fragment key={component.uid}>
<br />
Uid: {component.uid}
</Fragment>
);
}

function printCurveInfo(curve: LogCurveInfo): JSX.Element {
const isDepthIndex = !!curve.maxDepthIndex;
return (
<Fragment key={curve.mnemonic}>
<br />
Mnemonic: {curve.mnemonic}
<br />
Start index: {isDepthIndex ? curve.minDepthIndex : curve.minDateTimeIndex}
<br />
End index: {isDepthIndex ? curve.maxDepthIndex : curve.maxDateTimeIndex}
</Fragment>
);
}
Loading

0 comments on commit 1168469

Please sign in to comment.