Author Topic: SDK to set Themes and Extensions  (Read 3898 times)

Offline aussieadam

  • Full Member
  • ***
  • Join Date: Mar 2017
  • Posts: 28
  • Forum Citizenship: +2/-0
SDK to set Themes and Extensions
« on: 23 May 2017 07:30:25 am »
Hey All,

I've been looking into the c11 sdk for setting themes and extensions and have been able to upload them using the sdk.

My question is, does anybody know where the default for themes/extensions would be set for in the sdk? i see using the portal we do it in the view section, however i can't seem to find that section in the content explorer for the SDK.

Any help would be appreciated.

Offline srilakshmi583

  • Associate
  • **
  • Join Date: Sep 2017
  • Posts: 1
  • Forum Citizenship: +0/-0
Re: SDK to set Themes and Extensions
« Reply #1 on: 18 Sep 2017 02:32:22 am »
Hi,

could you please provide us the SDK code on how we can import the theme?

Thanks.

Offline aussieadam

  • Full Member
  • ***
  • Join Date: Mar 2017
  • Posts: 28
  • Forum Citizenship: +2/-0
Re: SDK to set Themes and Extensions
« Reply #2 on: 19 Sep 2017 08:27:41 am »
Sure thing,

Make sure you have the SDK version that matches your cognos version. My below code will look at a directory for themes and upload anything in that directory. Also, as an update, we figured out how to set default themes/extension besides the default login page. If anyone has any idea how to do default login page, i'd still love to know. You can edit the default userProfileSettings and add the following: profile.setUserProfileSettings(new AnyTypeProp(null,
                    "{\"ui_homePage\":{\"perspective\":\"homepageName\"},\"ui_theme\":\"themeName\"}"));

Code for theme upload:

public class MainApp {
static ContentManagerService_portType cmService;

   //providing constructor to overwrite implicit public one
  private MainApp() {}

  public static void main(final String[] args){
        //connect to cognos
        final CognosConnection conn = new CognosConnection(dispatcherURL, nameSpaceId, userName, password);
        conn.connectToCognos();
        cmService = conn.getCmService();

        final SearchPathSingleObject spo = new SearchPathSingleObject();
        spo.set_value("/catalog/catalogFolder[@name='Themes']");

        //code for themes
        addCustomization(spo, themesPath, "theme");

        //code for extentions
        spo.set_value("/catalog/catalogFolder[@name='Extensions']");
        addCustomization(spo, extensionsDirectory, "extension");

        conn.logoffFromCognos();

    }

    private static void addCustomization(final SearchPathSingleObject spo, final String dir,
            final String resourceType) throws RemoteException, FileNotFoundException {
        final Customization customization = new Customization();
        final File file = new File(dir);
        final Base64BinaryMIMEProp customizationData = new Base64BinaryMIMEProp();
        byte[] customizationBytes;
        final File[] directoryListing = file.listFiles();

        // iterate through target Directory for any theme/extension
        if (directoryListing != null) {
            for (final File child : directoryListing) {

                final String filePath = child.getPath();
                final String fileName = child.getName();

                // convert the file content into a byte array
                customizationBytes = getByteStream(filePath);

                customizationData.setValue(customizationBytes);
                customization.setData(customizationData);
                final StringProp dataType = new StringProp();
                dataType.setValue("application/octet-stream");
                customization.setDataType(dataType);
                customization.setResourceType(new StringProp(null, resourceType));
                customization.setDefaultName(new TokenProp(null, fileName.substring(0, fileName.lastIndexOf('.'))));
                addBaseClass(spo, customization);
            }
        }
    }

    private static void addBaseClass(final SearchPathSingleObject sps, final UiClass base) throws RemoteException {
        try {
            final AddOptions addOptions = new AddOptions();
            addOptions.setUpdateAction(UpdateActionEnum.replace);
            cmService.add(sps, new BaseClass[] { base }, addOptions);
            log.info("[{}] add sucessful", base.getClass());
        } catch (final RemoteException e) {
            log.error("Failed to adding element: [{}] ", base.getClass());
            throw e;
        }
    }

    private static byte[] getByteStream(final String filePath) throws FileNotFoundException {
        byte[] bytes = null;
        final FileInputStream fileInuptStream = new FileInputStream(new File(filePath));
        final BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInuptStream);
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            final int start = 0;
            final int length = 1024;
            int offset;
            final byte[] buffer = new byte[length];

            while ((offset = bufferedInputStream.read(buffer, start, length)) != -1) {
                byteArrayOutputStream.write(buffer, start, offset);
            }

            bytes = byteArrayOutputStream.toByteArray();

            bufferedInputStream.close();
            byteArrayOutputStream.flush();
            byteArrayOutputStream.close();
        } catch (final IOException e) {
            log.error("Excpetion: ", e);
        }
        return bytes;
    }
}

let me know if you have any questions
« Last Edit: 19 Sep 2017 08:29:49 am by aussieadam »