cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to manage Images through Java API

Former Member
0 Kudos

Hello everybody,

I'm trying to import Image in a repository through Java API, but I'm unable to create any data groups. The MDM4J Javadoc is poor and I can't find which method/class to use. Any thoughts ?

Vianney Cau.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Nir,

In your example you say that you are adding a new image to the "Images" field for the "S0698597" sku.

When you say "adding a new image to the Images field", do you mean hereby that you are editing the Image lookup field to link the image to the object?

Like your example shows, first you need to add the image to the Image table. This is done with the following piece of code if I'm correct:

int newImageId = this.catalogData.AddRecord("Images", a2iFields, 0, 0);

And then you link the image with the following code:

(using the id that is stored in the newImageId variable in the above code)

a2iValueArray.Add(new Value(newImageId));

a2iFields.Add(new A2iField("Images", new Value(a2iValueArray)));

this.catalogData.UpdateRecord(Schema.Products.TABLE, recordID, changeStamp, a2iFields);

Am I correct if I interpret this as doing the following:

"directly accessing the image lookup field of the S0698597 sku object and copying the internal id of the image into this lookup field".

Also I'm wondering why you are using a multi valued field.

So you can have (add) multiple images for the object I suppose?

And last but not least, are you familiar with using image variants?

Because I'm looking for a way to add images similar to the process of your example and at the same time associating certain (predefined) variants with those images.

This way, it would be possible to write a program that adds images to the image table, links them to records and automatically associates them with variant definitions so that MDM generates different versions of them.

Former Member
0 Kudos

Hi Joeri,

I'll try to answer all your questions, so look for my in-line comments...

<i>> Hi Nir,

>

> In your example you say that you are adding a new

> image to the "Images" field for the "S0698597" sku.

>

> When you say "adding a new image to the Images

> field", do you mean hereby that you are editing the

> Image lookup field to link the image to the object?</i>

Yes I am. I am linking the image as this specific SKU's image field, by setting the lookup ID to match the ID of the imported image.

<i>>

> Like your example shows, first you need to add the

> image to the Image table. This is done with the

> following piece of code if I'm correct:

>

> int newImageId =

> mageId = this.catalogData.AddRecord("Images",

> a2iFields, 0, 0);

>

>

> And then you link the image with the following code:

> (using the id that is stored in the newImageId

> variable in the above code)

>

> a2iValueArray.Add(new Value(newImageId));

> a2iFields.Add(new A2iField("Images", new

> es", new Value(a2iValueArray)));

>

>

>

>

>

>

>

> this.catalogData.UpdateRecord(Schema.Products.TABLE,

> , recordID, changeStamp, a2iFields);

>

> Am I correct if I interpret this as doing the

> following:

>

> "directly accessing the image lookup field of the

> S0698597 sku object and copying the internal id of

> the image into this lookup field".

></i>

Yes. I believe you got that right.

<i>>

> Also I'm wondering why you are using a multi valued

> field.

>

> So you can have (add) multiple images for the object

> I suppose?

></i>

Right again.

<i>>

> And last but not least, are you familiar with using

> image variants?

>

> Because I'm looking for a way to add images similar

> to the process of your example and at the same time

> associating certain (predefined) variants with those

> images.

>

> This way, it would be possible to write a program

> that adds images to the image table, links them to

> records and automatically associates them with

> variant definitions so that MDM generates different

> versions of them.</i>

You sure did save the hardest one to the end...

Well, as much as I know, the ONLY way to define or create image variants is through <i>MDM Image Manager</i>. You can only GET the variant, if exists for the image you want to link.

If you can further describe the exact scenario of what you are trying to do, specifying which of the tasks should be executed by a program and which of them are manually made, I can try to further assist you.

Regards,

Nir

Former Member
0 Kudos

Nir,

first of all, thank you for the time and effort you put in reading my post and answering all of my questions.

For the image variants part, I'll explain a bit more what we want to achieve.

It's not our goal to automate the defining (creation) of the image variants.

We actually only need to define a few variant definitions (which I think is the case in most of the MDM implementations).

So it's no problem that this can only be done through the MDM Image Manager.

But what we DO want to have automated is the association of predefined (via the MDM Image Manager) variants "at runtime" with images that are being added to the MDM repository.

This way we could have an almost fully automated process of loading new images in MDM.

Something like this:

Periodically a program is run which checks for all records in the main table, wether or not a picture is available (by checking if there is a value in the image lookup field).

For every record without an image, the record id is extracted to a sheet (for example an Excel file).

This list is used when making new images for those records. In a second column, the filename is manually added for every record in the sheet.

This file would be used by a custom program that cycles through the list and

- loads the image to the images table in MDM

- links this image to the image lookup field of the corresponding object. This can be achieved because of the data in the sheet.

- associates the predefined variant definitions with this image lookup field.

It is this last step that my question was about.

Because in the console reference guide I read the following:

"...instead, each image lookup field in the repository has a Variants property that allows you to associate one or more variants with the image lookup field, and in so doing, identify which variants should be generated when an image is associated with that particular field."

But I didn't find how to do this through the API in the Java API reference guide.

Regards,

Joeri

Former Member
0 Kudos

Hi Joeri.

Ok, let us agree on a few facts regarding <i>Image Variants</i> in the MDM environment:

1. In the <i>MDM Console</i> you can associate a certain image variant (1 or more) to a specific image lookup field (you cannot change it on runtime, or when the repository is loaded).

2. You cannot generate the actual variants through the API. The only possible way is through MDM Image Manager.

Now, if I understand your scenario well, you have to create 2 programs using the API:

1. A program which cycles through the main table and looks for records with no image linked to them, and creates a list in an outside file, containing enough information to recognize the record.

2. A program which cycles through the list, gets the filepath of the matching image for each record, loads the image to the repository, and links it to the record.

I believe that you know and able to write these two programs. Please let me know if you have any problems doing so.

Another step in the process has to be done in the <i>MDM Image Manager</i>. Once in a while, someone have to go inside the program, look for "<b>Required, Not Done</b>" variants, and generate them.

There's no way I'm familiar with to automate this step.

If you want to "remember" which of the pre-defined variants you've set for your image lookup field should be used in real-time, you can create a calculated field in the main table, that holds the variant's name, according to some criteria you'll define for this field.

Other way of doing it is to have a simple text field that holds the variant name, which you will fill programmatically, when linking the image to the record.

I truly hope this answers your questions.

Please let me know if you need any further assistance.

Best regards,

Nir

Former Member
0 Kudos

I think that the level of automation we have in mind regarding image variants is not achievable with the current possibilities.

Thank you for the explanation Nir.

Former Member
0 Kudos

How do you get an image variant from the API. I tried the following with the COM API:

CachedImageInfo imgInfo = catalog.GetCachedImageInfo("Images", webimgID, "Web Image");

I always get the error:

<i>Error retrieving image: hr=0x80004005, ImageID=58, VariantID=1</i>

Former Member
0 Kudos

Hi Vianney.

Please try and follow the following code sample:

    private void addImage() throws StringException, FileNotFoundException, IOException
    {
        /*
         To add image to field perform the following step
          1. add the image to "Image" table
          2. link the id of the new image into the field.
         */

        // for example: adding new image to the "Images" field for the
        // "S0698597" sku.

        // step 1 - adding the new image to the Image table
        //   1.1 prepare the A2iFields for image record. the A2iFields must
        //       contains the following fields:
        //      "DataObject" contains the image bytes
        //      "DataGroupId" contains the Data Group id, every text must link "to Data Group"
        //      "Name" contains the Name of the image
        //      "OrigLocationId" the location id of the image
        //      "Description" optional field. contain description on the image

        // prepare the a2iFields
        A2iFields a2iFields = new A2iFields();

        // adding the "DataObject" - read the the bytes from the file then store
        // it in variable
        String filePath = "C:\temp\image.jpg";
        File file = new File(filePath);
        byte[] bytes = new byte[(int) file.length()];
        // read byte from file, and set the large data
        FileInputStream inputStream = new FileInputStream(file);
        inputStream.read(bytes);
        a2iFields.Add(new A2iField("DataObject", new Value(bytes)));

        // adding the "DataGroupId"
        String dataGroupName = "DataGroup for testing images";
        // get the id of the dataGroupId, if the dataGroupName doesn't exist it
        // will be added by the method
        int dataGroupId = getDataGroupIdByName(dataGroupName);
        a2iFields.Add(new A2iField("DataGroupId", new Value(dataGroupId)));

        // adding the name
        a2iFields.Add(new A2iField("Name", new Value(file.getName())));
	a2iFields.Add(new A2iField("OrigName", new Value(file.getName())));


        // adding OrigLocationId
        int origLocationId = this.catalogData.AddDataLocation(filePath, 0, DataLocationType.RemovableMediaLocation);
        a2iFields.Add(new A2iField("OrigLocationId", new Value(origLocationId)));

        // adding Description
        a2iFields.Add(new A2iField("Description", new Value("Description")));

        // adding the text to the "Images" table
        int newImageId = this.catalogData.AddRecord("Images", a2iFields, 0, 0);

        // step 2 - link image id to the fields

        // get record id and change stamp from catalog
        int recordID =this.getRecordID(Schema.Products.TABLE,Schema.Products.SKU,"S0698597");
        int changeStamp =this.getChangeStamp(Schema.Products.TABLE, recordID);

        // prepare the A2iField to link the new id of the image to the Images fields
        a2iFields = new A2iFields();
        A2iValueArray a2iValueArray = new A2iValueArray(); // multi value field
        a2iValueArray.Add(new Value(newImageId));
        a2iFields.Add(new A2iField("Images", new Value(a2iValueArray)));
        this.catalogData.UpdateRecord(Schema.Products.TABLE, recordID, changeStamp, a2iFields);

    }

Hope this helps...

Nir