package org.alfresco.pruebas;
package org.alfresco.pruebas;
import java.io.IOException;
import java.rmi.RemoteException;
import javax.servlet.http.HttpServletRequest;
import org.alfresco.web.scripts.AbstractWebScript;
import org.alfresco.web.scripts.WebScriptRequest;
import org.alfresco.web.scripts.WebScriptResponse;
import org.alfresco.web.scripts.servlet.WebScriptServletRequest;
import org.alfresco.webservice.authentication.AuthenticationFault;
import org.alfresco.webservice.repository.RepositoryFault;
import org.alfresco.webservice.repository.UpdateResult;
import org.alfresco.webservice.types.CML;
import org.alfresco.webservice.types.CMLCreate;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.ParentReference;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.Utils;
import org.alfresco.webservice.util.WebServiceFactory;
public class PruebaWebScript extends AbstractWebScript
{
private String name1;
//private String name2;
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException
{
HttpServletRequest httpReq = ((WebScriptServletRequest)req).getHttpServletRequest();
// locate the root path
name1 = (String) httpReq.getParameter("name1");
//name2 = (String) httpReq.getParameter("name2");
System.out.println(name1);
//System.out.println(name2);
this.validarUsuario();
String dir = "/app:company_home/app:guest_home";
//String dir2 = "/app:company_home/app:guest_home/cm:"+name1;
this.crearEspacio(dir,name1);
//this.crearEspacio(dir2,name2);
this.terminarSesion();
}
private boolean iniciarSesion(){
try {
AuthenticationUtils.startSession("admin", "admin");
return true;
} catch (AuthenticationFault e) {
e.printStackTrace();
return false;
}
}
private boolean terminarSesion(){
AuthenticationUtils.endSession();
return true;
}
private boolean crearEspacio(String dir, String name){
System.out.println("Voy a crear un directorio en: " + dir);
// The default workspace store is SpacesStore
Store storeReference = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
// We want to create a new folder or workspace called child in the parent folder under guest home
ParentReference parentReference = new ParentReference(storeReference, null, dir ,
Constants.ASSOC_CONTAINS, Constants.createQNameString(Constants.NAMESPACE_CONTENT_MODEL, name));
NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(Constants.PROP_NAME, name) };
CMLCreate create = new CMLCreate("1", parentReference, null, null, null, Constants.TYPE_FOLDER, properties);
// Create the child folder, ATENCION: preparado para el prototipo "Especialidad"
CML cml = new CML();
cml.setCreate(new CMLCreate[] { create });
// inicializamos para que no de errores el try/catch
UpdateResult[] results = null;
try {
results = WebServiceFactory.getRepositoryService().update(cml);
} catch (RepositoryFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Obtnenemos la referencia al nuevo espacio y se imprime por consola
String childReference = results[0].getDestination().getUuid();
System.out.println("Created a new workspace, node is:" + childReference);
return true; // por añadir alguna linea de validación.
}
}
<webscript>
<shortname>The World's Simplest Webscript</shortname>
<description>Crea un directorio padre con un directorio hijo</description>
<url>/pruebas/prueba</url>
<authentication>none</authentication>
<format default="">argument</format>
</webscript>
<bean id="webscript.org.alfresco.pruebas.pruebas.get"
class="org.alfresco.pruebas.PruebaWebScript"
parent="webscript">
</bean>
package org.alfresco.module.demoscripts;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.model.Repository;
import org.alfresco.service.cmr.repository.NodeRef;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import org.springframework.extensions.webscripts.Status;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.namespace.QName;
public class CreateFolderWebScript extends AbstractWebScript{
private NodeService nodeService;
private Repository repository;
private FileFolderService fileFolderService;
public FileFolderService getFileFolderService() {
return fileFolderService;
}
public void setFileFolderService(FileFolderService fileFolderService) {
this.fileFolderService = fileFolderService;
}
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
public NodeService getNodeService() {
return nodeService;
}
public void setRepository (Repository repository){
this.repository = repository;
}
@Override
public void execute(WebScriptRequest req, WebScriptResponse res)
throws IOException {
//nueva carpeta
NodeRef newFolder = null;
//recogemos los parametros del nombre de la carpeta en este caso se llama path
String paramNameFolder = req.getParameter("path");
//Instanciamos el nodo companyhome
NodeRef companyHomeRef = repository.getCompanyHome();
//Instanciamos el nodo donde queremos crear la carpeta en este caso guest_home
NodeRef pathGuestHome = getFileFolderService().searchSimple(companyHomeRef, "Guest Home");
//si no encontramos el nodo lanzamos una exception
if (pathGuestHome==null) {
throw new WebScriptException(Status.STATUS_NOT_FOUND,
"Carpeta no encontrada");
}
else {
//lo he creado así se puede crear de muchas formas
String folderName = paramNameFolder + System.currentTimeMillis() ;
String folderTitle = "Titulo de mi Carpeta";
Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
contentProps.put(ContentModel.PROP_NAME, folderName);
contentProps.put(ContentModel.PROP_TITLE, folderTitle);
newFolder = nodeService.createNode(pathGuestHome, ContentModel.ASSOC_CONTAINS, QName.createQName("{http://www.alfresco.org/model/content/1.0}"
+ folderName), ContentModel.TYPE_FOLDER, contentProps).getChildRef();
}
try {
JSONObject obj = new JSONObject();
obj.put("nombre del parametro enviado", paramNameFolder);
obj.put("mi carpeta creada", newFolder.getId());
String jsonString = obj.toString();
res.getWriter().write(jsonString);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<!– crear carpeta con webscript Java–>
<bean id="webscript.org.alfresco.demo.createFolder.get"
class="org.alfresco.module.demoscripts.CreateFolderWebScript"
parent="webscript">
<property name="repository" ref="repositoryHelper" />
<property name="nodeService" ref="NodeService" />
<property name="fileFolderService" ref="fileFolderService" />
</bean>
<webscript>
<shortname>Crear una Carpeta Via WebScript Java</shortname>
<description>Crea una carpeta y devuelve un JSON</description>
<url>/demo/createFolder?path={path}</url>
<authentication>admin</authentication>
<format default="">argument</format>
</webscript>
Content from pre 2016 and from language groups that have been closed.
Content is read-only.
By using this site, you are agreeing to allow us to collect and use cookies as outlined in Alfresco’s Cookie Statement and Terms of Use (and you have a legitimate interest in Alfresco and our products, authorizing us to contact you in such methods). If you are not ok with these terms, please do not use this website.