/**
 * Copyright 2008-2009 DRIVER PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.data.sts.profile.utils;

import org.apache.log4j.Logger;

import eu.dnetlib.common.profile.ProfileHeader;
import eu.dnetlib.data.sts.profile.sdo.SdoProfileBody;
import eu.dnetlib.data.sts.profile.sdo.SdoProfileConfiguration;
import eu.dnetlib.data.sts.profile.sdo.SdoProfileStatus;


/**
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 * 
 */
public class SDOProfileMarshaller {

	protected static final Logger log = Logger
			.getLogger(SDOProfileMarshaller.class);

	/**
	 * Generates xml representation of ProfileHeader object.
	 * 
	 * @param profileHeader
	 * @return
	 */
	public static String generateProfileHeader(ProfileHeader profileHeader) {
		if (profileHeader == null)
			return null;
		StringBuffer strBuff = new StringBuffer("<HEADER>");
		strBuff.append("<RESOURCE_IDENTIFIER value=\""
				+ profileHeader.getResourceIdentifier() + "\"/>");
		strBuff.append("<RESOURCE_TYPE value=\""
				+ profileHeader.getResourceType() + "\"/>");
		strBuff.append("<RESOURCE_KIND value=\""
				+ profileHeader.getResourceKind() + "\"/>");
		if (profileHeader.getResourceURI() != null)
			strBuff.append("<RESOURCE_URI value=\""
					+ profileHeader.getResourceURI() + "\"/>");
		else
			strBuff.append("<RESOURCE_URI value=\"\"/>");

		if (profileHeader.getDateOfCreation() != null)
			strBuff.append("<DATE_OF_CREATION value=\""
					+ profileHeader.getDateOfCreation() + "\"/>");
		else
			strBuff.append("<DATE_OF_CREATION value=\"\"/>");

		strBuff.append("</HEADER>");
		return strBuff.toString();
	}
	
	/**
	 * Generates profile with body part and returns it as xml
	 * representation.
	 * 
	 * @param profileHeader
	 * @param storeProfileBody 
	 * @return xml profile representation
	 */
	public static String generateSDOProfile(ProfileHeader profileHeader,
			SdoProfileBody profileBody) {
		StringBuffer strBuff = new StringBuffer("<RESOURCE_PROFILE>");
		String profileHeaderPartStr = generateProfileHeader(profileHeader);
		if (profileHeaderPartStr != null)
			strBuff.append(profileHeaderPartStr);
		if (profileBody != null) {
			strBuff.append("<BODY>");
			String confPartStr = generateSDOConfPartOfProfile(profileBody
					.getConfiguration());
			if (confPartStr != null)
				strBuff.append(confPartStr);
			String statusPartStr = generateSDOStatusPartOfProfile(profileBody
					.getStatus());
			if (statusPartStr != null)
				strBuff.append(statusPartStr);
			strBuff.append("</BODY>");
		}
		strBuff.append("</RESOURCE_PROFILE>");

		return strBuff.toString();
	}

	
	/**
	 * Generates configuration part of sdo profile.
	 * 
	 * @param mdstoreConf
	 * @return xml representation of configuration part
	 */
	public static String generateSDOConfPartOfProfile(
			SdoProfileConfiguration storeConf) {
		if (storeConf == null)
			return null;
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<CONFIGURATION>");
	
		if (storeConf.getSdoMimeType() != null){
			strBuff.append("<SDO_MIME_TYPE>");
			strBuff.append(storeConf.getSdoMimeType());
			strBuff.append("</SDO_MIME_TYPE>");
		} else {
			strBuff.append("<SDO_MIME_TYPE/>");
		}		

		strBuff.append("</CONFIGURATION>");
		return strBuff.toString();
	}

	/**
	 * Generates status part of sdo profile.
	 * 
	 * @param storeStatus
	 * @return xml representation of status part
	 */
	public static String generateSDOStatusPartOfProfile(
			SdoProfileStatus storeStatus) {
		if (storeStatus == null)
			return null;
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<STATUS>");

		if (storeStatus.getLastModificationDate() != null) {
			strBuff.append("<LAST_MODIFICATION_DATE>");
			strBuff.append(storeStatus.getLastModificationDate());
			strBuff.append("</LAST_MODIFICATION_DATE>");
		} else
			strBuff.append("<LAST_MODIFICATION_DATE />");

		strBuff.append("<SDO_SIZE>");
		strBuff.append(storeStatus.getSdoSize());
		strBuff.append("</SDO_SIZE>");
		
		if (storeStatus.getSdoVersion() != null) {
			strBuff.append("<SDO_VERSION>");
			strBuff.append(storeStatus.getSdoVersion());
			strBuff.append("</SDO_VERSION>");
		} else
			strBuff.append("<SDO_VERSION/>");
		
		if (storeStatus.getSdoUri() != null) {
			strBuff.append("<SDO_URI>");
			strBuff.append(storeStatus.getSdoUri());
			strBuff.append("</SDO_URI>");
		} else
			strBuff.append("<SDO_VERSION/>");
		
		if (storeStatus.getSdo() != null) {
			strBuff.append("<SDO>");
			strBuff.append(storeStatus.getSdo());
			strBuff.append("</SDO>");
		} else
			strBuff.append("<SDO/>");
		
		strBuff.append("</STATUS>");
		return strBuff.toString();
	}
}
