6

I am new To tridion. I am trying a Java program which automatically uploads the image into tridion when an image is selected from the local machine. I have written the following code.

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.sdltridion.contentmanager.coreservice.CoreService2011;
import com.sdltridion.contentmanager.coreservice.ICoreService;
import com.sdltridion.contentmanager.coreservice.ICoreServiceGetSystemWideListCoreServiceFaultFaultFaultMessage;
import com.sdltridion.contentmanager.coreservice.IStreamUpload;
import com.sdltridion.contentmanager.r6.BinaryContentData;
import com.sdltridion.contentmanager.r6.ComponentData;
import com.sdltridion.contentmanager.r6.ComponentType;
import com.sdltridion.contentmanager.r6.IdentifiableObjectData;
import com.sdltridion.contentmanager.r6.ItemType;
import com.sdltridion.contentmanager.r6.LinkToMultimediaTypeData;
import com.sdltridion.contentmanager.r6.LinkToSchemaData;
import com.sdltridion.contentmanager.r6.ObjectFactory;
import com.sdltridion.contentmanager.r6.ReadOptions;

public class FileUpload {
    public static void main(String args[])
            throws ICoreServiceGetSystemWideListCoreServiceFaultFaultFaultMessage {
        CoreService2011 service = new CoreService2011();
        ICoreService client = service.getBasicHttp();


        ObjectFactory objectFactory = new ObjectFactory();
        String IdRefDec;


        try {

            ComponentData multimediaComponent = (ComponentData) client
                    .getDefaultData(ItemType.COMPONENT, "tcm:246-95-2");


            LinkToSchemaData linkToSchemaData = new LinkToSchemaData();
            linkToSchemaData.setIdRef(objectFactory.createLinkIdRef("tcm:246-243-8"));


            ComponentType componentType = null;
            componentType = componentType.MULTIMEDIA;
            multimediaComponent.setComponentType(objectFactory
                    .createComponentType(componentType));
            multimediaComponent.setTitle(objectFactory
                    .createLinkTitle("MMComponent"));

            multimediaComponent.setSchema(objectFactory.createLinkToSchemaData(linkToSchemaData));

            System.out.println(multimediaComponent.getSchema().toString());

            String myFilename = "Chrysanthemum.jpg";

            System.out.println("Myfile name done");

            String extension = myFilename
                    .substring(myFilename.lastIndexOf('.') + 1);
            System.out.println("extension: " + extension);

            String tempPath = "";

            IStreamUpload iStreamUpload = service.getStreamUploadBasicHttp();
            System.out.println("istreamupload");

            // UploadRequest uploadRequest = new UploadRequest();

            File file = new File(
                    "C:\\Users\\xxx\\Desktop\\images\\Chrysanthemum.jpg");
            String s = file.toString();

            byte[] myByte = new byte[4096];

            InputStream ios = new FileInputStream(file);

            ByteArrayOutputStream ous = null;

            try {

                ous = new ByteArrayOutputStream();

                int read = 0;

                while ((read = ios.read(myByte)) != -1) {

                    ous.write(myByte, 0, read);

                }

            } finally {

                if (ous != null)

                    ous.close();

                if (ios != null)

                    ios.close();

            }

            // uploadRequest.setUploadContent(ous.toByteArray());

            tempPath = iStreamUpload.uploadBinaryByteArray(s

            , myByte);

            System.out.println(tempPath);

            System.out.println(multimediaComponent.getSchema().getName());

            BinaryContentData binaryContentData = new BinaryContentData();
            binaryContentData.setUploadFromFile(objectFactory
                    .createLinkTitle(tempPath));
            binaryContentData.setFilename(objectFactory
                    .createBinaryContentDataFilename(myFilename));
            System.out
                    .println(binaryContentData.getUploadFromFile().getValue());

            // if (extension == ".png")
            // {
            // IdRefDec = "tcm:0-3-65544";
            //
            // }
            // else if (extension == ".jpg" || extension == ".jpeg")
            // {
            // IdRefDec = "tcm:0-2-65544";
            //
            // }
            // else if (extension.equalsIgnoreCase(".jpg") ||
            // extension.equalsIgnoreCase(".jpeg"))
            // {
            // IdRefDec = "tcm:0-2-65544";
            //
            // }
            // else if (extension == ".bmp")
            // {
            // IdRefDec = "tcm:0-13-65544";
            //
            // }
            // else if (extension == ".gif")
            // {
            // IdRefDec = "tcm:0-1-65544";
            //
            // }
            // else if (extension == ".ico")
            // {
            // IdRefDec = "tcm:0-35-65544";
            //
            // }
            // else
            // {
            // IdRefDec = "";
            // }

            LinkToMultimediaTypeData multimediaTypeData = new LinkToMultimediaTypeData();
            multimediaTypeData.setIdRef(objectFactory
                    .createLinkIdRef("tcm:0-2-65544"));


            binaryContentData.setMultimediaType(objectFactory
                    .createLinkToMultimediaTypeData(multimediaTypeData));
            multimediaComponent.setBinaryContent(objectFactory
                    .createBinaryContentData(binaryContentData));
            IdentifiableObjectData savedComponent = client.create(
                    multimediaComponent, new ReadOptions());
            savedComponent.getId();
            // client.checkIn(savedComponent.getId(), new ReadOptions());

        } catch (Exception ex) {

        ex.printStackTrace();

        }

    }
  }

When I try to run the above code, it is giving the below exception

Exception :
com.sdltridion.contentmanager.coreservice.ICoreServiceCreateCoreServiceFaultFaultFaultMessage: Unable to get Schema of Component.

Can any one help me in solving this issue.

Thanks in advance........

5
  • Does the schema 'tcm:246-243-8' exist?
    – johnwinter
    May 2 '13 at 13:22
  • Can you show the results of the logging you've coded - this would help to confirm where exactly in the code it is failing? May 2 '13 at 16:11
  • @johnwinter : Yes john the schema exists. May 3 '13 at 4:08
  • Refer this stack overflow question: tridion.stackexchange.com/questions/1142/… It may help you..
    – user467
    May 4 '13 at 16:57
  • @Deepika : Thanks for your post. Actually, I have only posted that question on how to set the link to schema. May 6 '13 at 3:27
2

I'm not familiar with the Java proxy you are using, but it looks a bit odd that you already have a LinkToSchemaData, and then instead of using it directly, you pass it to createLinkToSchemaData().

1
  • LinkToSchemaData linkToSchemaData = new LinkToSchemaData(); linkToSchemaData.setIdRef(objectFactory.createLinkIdRef("tcm:246-243-8")); multimediaComponent.setSchema(objectFactory.createLinkToSchemaData(linkToSchemaData)); LinkToSchemaData is a class in Tridion and CreateLinkToSchemaData is a method in ObjectFactory which we use when we want to create a JAXBElement. Here the linkToSchemaData.setIdRef(objectFactory.createLinkIdRef("tcm:246-243-8")); accepts JAXBElement so I have used that statement. May 3 '13 at 5:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.