cancel
Showing results for 
Search instead for 
Did you mean: 

Read a text in KM

Former Member
0 Kudos

I hate to ask this because I see at least 5 posts related to this, however I cannot get any of the examples to work except one which does not apply in my case.

I am trying to read the file in KM from a MetaData Extension.

I cannot read the file using the URL class.

Doesn't SAP provide an easy way to read a file from KM?

If someone can help me out I would appreciate it. Down in the method "getAllowedValues" is where I have to read the file.


package com.hersheys.all;
 
import java.util.Locale;
import java.util.ResourceBundle;
import com.sap.tc.logging.Location;
import com.sapportals.portal.prt.component.IPortalComponentRequest;
import com.sapportals.wcm.service.propertyconfig.IMetaContext;
import com.sapportals.wcm.service.propertyconfig.IMetaModel;
import com.sapportals.wcm.service.propertyconfig.IMetaName;
import com.sapportals.wcm.service.propertyconfig.IMetaValue;
import com.sapportals.wcm.service.propertyconfig.IMetaValueList;
import com.sapportals.wcm.service.propertyconfig.PropertyConfigurationServiceException;
import com.sapportals.wcm.service.propertyconfig.metadataextension.IDynamicValues;


import javax.servlet.http.HttpServletRequest;


import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;



import com.sap.security.api.IUser;
import com.sap.security.api.UMFactory;
import com.sap.security.api.umap.IUserMappingData;
import com.sap.security.api.umap.NoLogonDataAvailableException;

public class DynamicMetadataProvider implements IDynamicValues 
{
	private static final Location log = Location.getLocation(DynamicMetadataProvider.class);
	private IMetaModel model = null;

	public DynamicMetadataProvider(IMetaModel model) 
	{
	  	this.model = model;
	}

	/**
	 * Gets a label for a property value depending on the input locale.
	 * @param metaValue metaValue for that a label is requested
	 * @param locale locale to determine the lable language
	 * @return label for a property value, returns null if no label can be found for the value
	 */
	public String getValueLabel(IMetaValue metaValue, Locale locale) throws PropertyConfigurationServiceException 
	{
		String value = metaValue.getValue();
		try 
		{
			value = ResourceBundle.getBundle(this.getClass().getName(), locale).getString("value.lbl."+value);
		} 
		catch (Exception ex) 
		{
			log.errorT("getValueLabel", ex.getMessage());
		}
		return value;
	}

	/**
	 * Returns values depending on the meta name.
	 * @param metaName meta model information of a property
	 * @return list of relevant values (incl. empty list) or null if there is no value domain
	 */
	public IMetaValueList getAllowedValues(IMetaName metaName) throws PropertyConfigurationServiceException 
	{
	  return this.getAllowedValues(metaName, null);
	}
	
	/**
	 * Returns values depending on the meta name and the resource.
	 * @param metaName meta model information of a property
	 * @param metaContext context information
	 * @return list of relevant values (incl. empty list) or null if there is no value domain
	 */
	public IMetaValueList getAllowedValues(IMetaName metaName, IMetaContext metaContext) throws PropertyConfigurationServiceException 
	{
		IMetaValueList valueList = this.model.createEmptyMetaValueList();

                //Here is where I need to read the text file and set the value to the MetaData Extension like this:  valueList.add(model.createMetaValue(model, metaName, this, "string of value to add"));
	}
	/**
	 * Returns a default value depending on the meta name.
	 * @param metaName      meta model information of a property
	 * @return default value or null
	 */
	public IMetaValue getDefaultValue(IMetaName metaName) 
	{
	  return this.model.createMetaValue(this.model, metaName, this, "");
	}

	/**
	 * Gets a label for the property name depending on the input locale.
	 * @param metaName meta name for that the label is requested
	 * @param locale locale to determine the lable language
	 * @return label for a property name, returns null if no label can be found for the name
	 */
	public String getPropertyLabel(IMetaName metaName, Locale locale) throws PropertyConfigurationServiceException 
	{
		String value = metaName.getName();
		try 
		{
			value = ResourceBundle.getBundle(this.getClass().getName(), locale).getString("property.lbl."+value);
		} 
		catch (Exception ex) 
		{
			log.errorT("getPropertyLabel", ex.getMessage());
		}
		return value;
	}

	/**
	 * Gets a description for a property depending on the input locale.
	 * @param metaName meta name for that the description is requested
	 * @param locale locale to determine the description language
	 * @return description for a property, returns null if no description can be found for the property
	 */
	public String getPropertyDescription(IMetaName metaName, Locale locale) throws PropertyConfigurationServiceException 
	{
		String value = metaName.getName();
		try 
		{
			value = ResourceBundle.getBundle(this.getClass().getName(), locale).getString("property.desc."+value);
		} 
		catch (Exception ex) 
		{
			log.errorT("getPropertyDescription", ex.getMessage());
		}
		return value;
	}

	public IMetaValue searchMetaValue(String value) throws PropertyConfigurationServiceException 
	{
		return null;
	}
}

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

If the file is in KM, you need to obtain an

IResource 

object for it from the

ResourceFactory

, and then call

getContent()

on it.

Best regards, Julian

Answers (1)

Answers (1)

Former Member
0 Kudos

Here is my working code incase anyone else would like to see exactly how I did this.


public IMetaValueList getAllowedValues(IMetaName metaName, IMetaContext metaContext) throws PropertyConfigurationServiceException 
{
	IMetaValueList valueList = this.model.createEmptyMetaValueList();

		
	try
	{
		RID rid = RID.getRID("/documents/MetaDataStore/" + metaName.getName() + ".txt");
	
		IResourceContext rContext = metaContext.getResourceContext();
			
		com.sapportals.wcm.repository.IResource resource;
	
		resource = ResourceFactory.getInstance().getResource(rid, rContext);
	
		IContent content = resource.getContent();
			
		BufferedReader in = new BufferedReader(new InputStreamReader(content.getInputStream()));
			
		String valuesFromFile = "";
			
		valuesFromFile = in.readLine();
			
		String[] values = valuesFromFile.split(";");
			
			
		for(int i=0;i<values.length;i++)
		{
			valueList.add(model.createMetaValue(model, metaName, this, values<i>));
		}
		
	} 
	catch(Exception e)
	{
		valueList.add(model.createMetaValue(model, metaName, this, "NO_VALUE_FOUND"));
	}
	return valueList;
}

Former Member
0 Kudos

Don't forget to close the stream when you're done. Relying on the garbage collection isn't a good idea...

Best regards, Julian

Former Member
0 Kudos

Your right, i added the content.close() and in.close(). Thanks again.