All files / src/impl/utils document.ts

13.86% Statements 14/101
0% Branches 0/12
0% Functions 0/30
15.56% Lines 14/90

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332                              7x                                     7x                                       7x                                             7x                                   7x                             7x                                               7x                                       7x                                     7x                                               7x                                                                                         7x                                                   7x                                                                         7x                             7x                      
/*
    Copyright (C) 2017 Red Hat, 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 {
  CollectionType,
  CommonUtil,
  ConfigModel,
  DocumentDefinition,
  ErrorInfo,
  ErrorLevel,
  ErrorScope,
  ErrorType,
  NamespaceModel,
} from '@atlasmap/core';
 
/**
 * Modify the document name of the document specified by the document ID.
 *
 * @param docId
 * @param newDocName
 * @param isSource
 */
export async function changeDocumentName(
  docId: string,
  newDocName: string,
  isSource: boolean,
) {
  const cfg = ConfigModel.getConfig();
  const docDef = getDocDef(docId, cfg, isSource);
  docDef.name = newDocName;
  await cfg.mappingService.notifyMappingUpdated();
  await cfg.fileService.updateDigestFile();
}
 
/**
 * Create a new namespace for the supplied XML document.
 *
 * @param docName
 * @param alias
 * @param uri
 * @param locationUri
 */
export function createNamespace(
  docName: string,
  alias: string,
  uri: string,
  locationUri: string,
  isTarget: boolean,
) {
  const cfg = ConfigModel.getConfig();
  const docDef = getDocDefByName(docName, cfg, true);
  const namespace: NamespaceModel = {
    alias: alias,
    uri: uri,
    locationUri: locationUri,
    createdByUser: true,
    isTarget: isTarget,
    getPrettyLabel: () => alias + ' [' + uri + ']',
    copy: () => Object.assign({}, namespace),
    copyFrom: (n: NamespaceModel) => Object.assign(namespace, n),
  };
  docDef.namespaces.push(namespace);
  cfg.mappingService.notifyMappingUpdated();
}
 
export function editNamespace(
  docName: string,
  initAlias: string,
  alias: string,
  uri: string,
  locationUri: string,
  isTarget: boolean,
) {
  const cfg = ConfigModel.getConfig();
  const docDef = getDocDefByName(docName, cfg, true);
  const namespace = docDef.getNamespaceForAlias(initAlias);
  namespace.alias = alias;
  namespace.uri = uri;
  namespace.locationUri = locationUri;
  namespace.isTarget = isTarget;
  cfg.mappingService.notifyMappingUpdated();
}
 
export function deleteNamespace(docName: string, alias: string) {
  const cfg = ConfigModel.getConfig();
  const docDef = getDocDefByName(docName, cfg, true);
  docDef.namespaces = docDef.namespaces.filter(
    (namespace: { alias: string }) => namespace.alias !== alias,
  );
  cfg.mappingService.notifyMappingUpdated();
}
 
/**
 * Remove a document from the UI and backend service.
 *
 * @param docDef
 * @param cfg
 */
export async function removeDocumentRef(
  docDef: DocumentDefinition,
  cfg: ConfigModel,
): Promise<boolean> {
  return new Promise<boolean>(async (resolve) => {
    cfg.mappingService.removeDocumentReferenceFromAllMappings(docDef.id);
    if (docDef.isSource) {
      CommonUtil.removeItemFromArray(docDef, cfg.sourceDocs);
    } else {
      CommonUtil.removeItemFromArray(docDef, cfg.targetDocs);
    }
    await cfg.mappingService.notifyMappingUpdated();
    await cfg.fileService.updateDigestFile();
    resolve(true);
  });
}
 
/**
 * Return the document definition associated with the specified document ID.
 *
 * @param docId - document ID
 * @param cfg
 * @param isSource
 */
export function getDocDef(
  docId: string,
  cfg: ConfigModel,
  isSource: boolean,
): DocumentDefinition {
  for (const docDef of cfg.getDocs(isSource)) {
    if (docDef.id.match(docId)) {
      return docDef;
    }
  }
  return null as unknown as DocumentDefinition;
}
 
/**
 * Return the document definition associated with the specified document name.
 *
 * @param docName - document name
 * @param cfg
 * @param isSource
 */
export function getDocDefByName(
  docName: string,
  cfg: ConfigModel,
  isSource: boolean,
): DocumentDefinition {
  for (const docDef of cfg.getDocs(isSource)) {
    const candidateDocName =
      docDef.getName(false) + '.' + docDef.type.toLowerCase();
    if (candidateDocName.match(docName)) {
      return docDef;
    }
  }
  return null as unknown as DocumentDefinition;
}
 
/**
 * Determine the user-defined class names associated with previously
 * imported JARs.
 */
export async function getCustomClassNameOptions(): Promise<string[]> {
  return new Promise<string[]>(async (resolve, reject) => {
    const cfg = ConfigModel.getConfig();
    cfg.documentService
      .getLibraryClassNames()
      .then((classNames: string[]) => {
        resolve(classNames);
      })
      .catch(() => {
        reject();
      });
  });
}
 
/**
 * Import a CSV, instance or schema document into either the Source panel or Target
 * panel (CSV, JSON, XML, XSD).
 *
 * @param selectedFile
 * @param cfg
 * @param isSource
 * @param isSchema - user-specified instance/ schema (true === schema)
 * @param inspectionParameters - CSV parameters
 */
export async function importInstanceSchema(
  selectedFile: File,
  cfg: ConfigModel,
  isSource: boolean,
  isSchema: boolean,
  inspectionParameters?: { [key: string]: string },
) {
  return new Promise<boolean>(async (resolve) => {
    cfg.initCfg.initialized = false;
    cfg.initializationService.updateLoadingStatus(
      'Importing Document ' + selectedFile.name,
    );
    cfg.documentService
      .importNonJavaDocument(
        selectedFile,
        isSource,
        isSchema,
        inspectionParameters,
      )
      .then(() => {
        cfg.fileService.updateDigestFile().finally(() => {
          cfg.initializationService.updateStatus();
          cfg.errorService.addError(
            new ErrorInfo({
              message: `${selectedFile.name} import complete.`,
              level: ErrorLevel.INFO,
              scope: ErrorScope.APPLICATION,
              type: ErrorType.USER,
            }),
          );
          resolve(true);
        });
      });
  });
}
 
/**
 * Enable the specified class name and collection type in the targetted
 * panel for use in Java document loading. The user must have previously
 * imported a JAR file containing the class.
 *
 * @param selectedClass
 * @param selectedCollection
 * @param isSource
 */
export function enableCustomClass(
  cfg: ConfigModel,
  selectedClass: string,
  selectedCollection: string,
  isSource: boolean,
) {
  return new Promise<boolean>((resolve) => {
    cfg.initCfg.initialized = false;
    cfg.initializationService.updateLoadingStatus(
      'Importing Document ' + selectedClass,
    );
    cfg.documentService
      .importJavaDocument(
        selectedClass,
        isSource,
        selectedCollection as CollectionType,
      )
      .then(() => {
        cfg.fileService.updateDigestFile().finally(() => {
          cfg.initializationService.updateStatus();
          resolve(true);
        });
      });
  });
}
 
export function getPropertyScopeOptions(isSource: boolean): {
  value: string;
  label: string;
}[] {
  const cfg = ConfigModel.getConfig();
  let scopeOptions: {
    value: string;
    label: string;
  }[] = [
    {
      label: 'Current Message Header',
      value: 'current',
    },
    {
      label: 'Camel Exchange Property',
      value: 'camelExchangeProperty',
    },
  ];
  const propertyDocOptions: DocumentDefinition[] = isSource
    ? cfg.sourceDocs
    : cfg.targetDocs;
  for (let propertyDocOption of propertyDocOptions) {
    scopeOptions.push({
      value: propertyDocOption.id,
      label: propertyDocOption.name,
    });
  }
  return scopeOptions;
}
 
/**
 * Return CSV document inspection parameters.
 *
 * @param docId
 * @param isSource
 * @returns
 */
export function getDocCSVParams(
  docId: string,
  isSource: boolean,
): { [key: string]: string } {
  const doc = getDocDef(docId, ConfigModel.getConfig(), isSource);
  return doc?.inspectionParameters;
}
 
/**
 * Set CSV document inspection parameters.
 *
 * @param docId
 * @param isSource
 * @param parameters
 */
export async function setDocCSVParams(
  docId: string,
  isSource: boolean,
  parameters: { [key: string]: string },
) {
  const cfg = ConfigModel.getConfig();
  const docDef = getDocDef(docId, cfg, isSource);
  docDef.inspectionParameters = parameters;
  await cfg.mappingService.notifyMappingUpdated();
  await cfg.fileService.updateDigestFile();
}