/**
 *
 */

package CH.ifa.draw.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import de.renew.draw.storables.ontology.Drawing;
import de.renew.draw.storables.ontology.Storable;
import de.renew.util.ObjectInputStreamUsingBottomLoader;


/**
 * This class contains utility methods for convenience handling of <code>Drawing</code>.
 *
 * @author cabac
 *
 * @deprecated This class is not to be used externally. Please use {@link de.renew.draw.storables.api.StorableApi} instead.
 */
@Deprecated
public class DrawingHelper {

    /**
     * Logger for the class DrawingHelper.
     * Only a single logger instance exists at any given time. If a logger for DrawingHelper
     * already exists it is returned or else a new instance is created.
     */
    public static final org.apache.log4j.Logger LOGGER =
        org.apache.log4j.Logger.getLogger(DrawingHelper.class);

    /**
     * Clones the drawing.
     *
     * @param originalDrawing the drawing to be cloned
     * @return a deep clone of the original drawing
     * @throws Exception thrown if cloning of drawing wasn't successful
     * @deprecated This method is not to be used externally. Please use the method
     * {@link de.renew.draw.storables.api.StorableApi#cloneStorable(Storable)} instead.
     */
    @Deprecated
    public static Drawing cloneDrawing(Drawing originalDrawing) throws Exception {
        Drawing drawing;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(originalDrawing);
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            ObjectInputStream ois = new ObjectInputStreamUsingBottomLoader(bais);
            Object deserializedObject = ois.readObject();
            drawing = (Drawing) deserializedObject;
        } catch (Exception e) {
            LOGGER.error("Could not clone drawing: " + originalDrawing.getName(), e);
            throw new Exception("Could not clone drawing: " + originalDrawing.getName(), e);
        }
        return drawing;
    }
}