03-01-2011 08:14 AM
04-28-2011 06:35 AM
02-14-2015 01:58 PM
package ec.com.uniseguros.util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.Repository;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.DocumentImpl;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.client.util.FileUtils;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
import org.apache.log4j.Logger;
import org.apache.poi.util.IOUtils;
import ec.com.uniseguros.exception.AlfrescoException;
/**
* @author chan
*
*/
public class AlfrescoUtils {
private static final Logger log = Logger.getLogger(AlfrescoUtils.class);
public static void endSession(final Session session) {
session.clear();
}
public static Session getSession(final String username,
final String password, final String url) throws AlfrescoException {
SessionFactory factory = SessionFactoryImpl.newInstance();
Map<String, String> parameter = new HashMap<String, String>();
// user credentials
parameter.put(SessionParameter.USER, username);
parameter.put(SessionParameter.PASSWORD, password);
// connection settings
parameter.put(SessionParameter.ATOMPUB_URL, url);
parameter.put(SessionParameter.BINDING_TYPE,
BindingType.ATOMPUB.value());
// create session
List<Repository> repositories = factory.getRepositories(parameter);
Session session = null;
if (repositories != null && !repositories.isEmpty()) {
session = repositories.get(0).createSession();
} else {
log.debug("No se encontro repositorio");
throw new AlfrescoException("No se encuentra repositorio");
}
return session;
}
public static Folder createChildFolder(final Folder parentFolder,
final String folderName) throws AlfrescoException {
try {
return FileUtils.createFolder(parentFolder, folderName,
"cmis:folder");
} catch (Exception e) {
log.debug("no se creo folder hijo");
throw new AlfrescoException(e);
}
}
public static Folder createRootFolder(final Session session,
final String folderName) throws AlfrescoException {
// FileUtils.getFolder(pathOrIdOfObject, session);
try {
Folder root = session.getRootFolder();
ObjectId parentId = session.createObjectId(root.getId());
// try to create the folder "test" which fails.
createMainFolder(session, parentId, folderName);
// Assuming we were able to create the folder (Done by using an
// existing folder and skipping the above creation)
CmisObject folderObj = null;
for (CmisObject childrens : root.getChildren()) {
if (childrens.getName().equals(folderName)) {
folderObj = childrens;
break;
}
}
Folder folder = (Folder) folderObj;
return folder;
} catch (Exception e) {
log.debug("no se creo carpeta root");
throw new AlfrescoException(e);
}
}
private static ObjectId createMainFolder(final Session session,
final ObjectId parentId, final String rootFolder)
throws AlfrescoException {
ObjectId mainFolderID = null;
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID,
BaseTypeId.CMIS_FOLDER.value());
properties.put(PropertyIds.NAME, rootFolder);
try {
mainFolderID = session.createFolder(properties, parentId);
} catch (Exception e) {
throw new AlfrescoException("No se pudo crear folder principal");
}
return mainFolderID;
}
public static String uploadDocument(final Folder folder,
final byte[] bytes, final String fileName, final String contentType) {
Map<String, String> newDocProps = new HashMap<String, String>();
newDocProps.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
newDocProps.put(PropertyIds.NAME, fileName);
ContentStream contentStream = new ContentStreamImpl(fileName, null,
contentType, new ByteArrayInputStream(bytes));
org.apache.chemistry.opencmis.client.api.Document doc = folder
.createDocument(newDocProps, contentStream,
VersioningState.NONE);
return doc.getId();
}
public static void deleteDocument(final Session session,
final String documentId) throws AlfrescoException {
CmisObject object = session.getObject(documentId);
if (object instanceof DocumentImpl) {
Document doc = (DocumentImpl) object;
doc.deleteAllVersions();
} else {
log.debug("No se encuentra documento para borrar");
throw new AlfrescoException("Documento no se encuentra");
}
}
public static ArchivoWrapper obtainByteArrayFromDocument(
final Session session, final String documentId)
throws AlfrescoException {
try {
CmisObject object = session.getObject(documentId);
if (object instanceof DocumentImpl) {
Document doc = (DocumentImpl) object;
ContentStream contentStream = doc.getContentStream();
ArchivoWrapper archivo = new ArchivoWrapper();
archivo.setArchivo(IOUtils.toByteArray(contentStream
.getStream()));
archivo.setNombre(contentStream.getFileName());
archivo.setContentType(contentStream.getMimeType());
return archivo;
} else {
throw new AlfrescoException("Documento no se encuentra");
}
} catch (IOException e) {
e.printStackTrace();
throw new AlfrescoException(e);
}
}
}
02-14-2015 04:33 PM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.