13.2.4. C.R.U.D.

Engineering Knowledge Manager

Administration Guide | Chapter 13: EKM Client API | 13.2. Using the Library in Client Applications |

13.2.3. Searching the EKM Repository |  | 13.2.5. Checking Permissions
Prev |  | Next

### 13.2.4. C.R.U.D.

C.R.U.D. stands for Create, Read, Update, and Delete. Searching for objects, as described previously, covers the Read portion of the acronym. The following section will cover the other portions.

#### Creating New Objects

The library provides operations for creating new objects in the repository. Each object in the repository has a type associated with it. For example, a folder in the repository is of the type `Folder`. When creating new objects in the repository, the application must specify the type of the object being created. The available types can be retrieved from the server as shown below:

Java

      Map<String, ModelSpec> types = dm.getModelSpecs();
      for (String type : types.keySet())
          System.out.println(">>>>> " + type + " : Label : " + types.get(type).getLabel());

Python (Jython)

      types = dm.getModelSpecs()
      for type in types:
          print ">>>>> " + type + " : Label : " + types[type].getLabel()

Python (IronPython)

      types = dm.GetModelSpecs()
      for type in types.Keys:
          print ">>>>> " + type + " : Label : " + types[type].Label

C#

      Dictionary<string, ModelSpec> specs = dm.GetModelSpecs();
      foreach (string type in specs.Keys)
          Console.WriteLine(">>>> " + type + " Label: " + specs[type].Label);

In the above examples, the type is the internal type name (for example, AdobePdfFile) and getLabel /Label returns a localized, human readable label (for example, Adobe Acrobat Document). Once the internal type name of a new object has been determined, it can be used to create the object. This is shown in the examples below.

Java

      dm.createModelObject("/Data/My Data" "My Workbench Projects", "Folder");

Python (Jython)

      dm.createModelObject("/Data/My Data" "My Workbench Projects", "Folder")

Python (IronPython)

      dm.CreateModelObject("/Data/My Data" "My Workbench Projects", “Folder”)

C#

      dm.CreateModelObject("/Data/My Data" "My Workbench Projects", “Folder”);

Since folders are one of the more likely object types to be created by the library, a convenience method is provided to do this.

Java

      dm.createFolder("/Data/My Data", "My Projects");

Python (Jython)

      dm.createFolder("/Data/My Data", "My Projects")

Python (IronPython)

      dm.CreateFolder("/Data/My Data", "My Projects")

C#

      dm.CreateFolder("/Data/My Data", "Test");

#### Modifying and Updating Objects

Once an object has been created or read from the server, the application can modify properties in the object. For example, you can modify an object’s description, set a user-defined property, and then update the object on the server. This is shown in the examples below.

Java

      1. ModelObject folder = dm.findByPath("/Data/My Data/My Projects");
      2. folder.setDescription("Folder for my personal projects");
      3. VariantFactory vf = conn.getVariantFactory();
      4. Variant value = vf.createVariant(25);
      5. folder.setProperty("maxNumberProjects", value);
      6. dm.updateModelObject(folder);

Python (Jython)

      1. folder = dm.findByPath("/Data/My Data/My Projects")
      2. folder.setDescription("Folder for my personal projects")
      3. vf = conn.getVariantFactory()
      4. value = vf.createVariant(25)
      5. folder.setProperty("maxNumberProjects", value)
      6. dm.updateModelObject(folder)

Let’s look at this example in more detail. All property values stored in an object in EKM are stored as a Variant. A Variant is a data structure that can store any type of data including strings, numbers, lists, and even references to other objects. Therefore, when you set a property using the `setProperty` operation, you must first create a Variant instance. This is done using the `VariantFactory`. This is shown on lines 3-5 in the examples above. Once the object has been modified, you must update it on the server as shown on line 6 in the examples above. Doing the above operation in C#/IronPython is slightly different.

Python (IronPython)

      1. folder = dm.FindByPath("/Data/My Data/My Projects")
      2. folder.SetUserProperty(“description”, VariantFactory.CreateVariant("My test folder"));
      3. value = VariantFactory.CreateVariant(25);
      4. if folder.Properties.ContainsKey("maxNumberProjects"):
      5.     folder.SetUserProperty("maxNumberProjects", value)
      6. else:
      7.     folder.AddProperty(ModelSpec.TAGS_CATEGORY, "maxNumberProjects", value);
      8. dm.UpdateModelObject(folder);

C#

      1. ModelObject folder = dm.FindByPath("/Data/My Data/My Projects");
      2. folder.SetUserProperty(“description, VariantFactory.CreateVariant("My test folder"));
      3. Variant value = VariantFactory.CreateVariant(25);
      4. if (!folder.Properties.ContainsKey("maxNumberProjects"))
      5.     folder.AddProperty(ModelSpec.TAGS_CATEGORY, "maxNumberProjects", value);
      6. else
      7.     folder.SetUserProperty("maxNumberProjects", value);
      8. dm.UpdateModelObject(folder);

The key difference here is that when setting a “user property” in C# or IronPython, you must first check to see if it exists, and add the property if it does not. This is shown on lines 4-7.

#### Deleting Objects

The function of deleting objects on the server is shown in the examples below.

Java

      dm.deleteModelObject("/Data/My Data/My Projects");

Python (Jython)

      dm.deleteModelObject("/Data/My Data/My Projects")

Python (IronPython)

      dm.DeleteModelObject("/Data/My Data/My Projects")

C#

      dm.DeleteModelObject("/Data/My Data/My Projects");

Contains proprietary and confidential information of ANSYS, Inc. and its subsidiaries and affiliates.

Prev | Up / Contents | Next

Release 18.2 - © ANSYS, Inc. All rights reserved.