edwinguedez
7/28/2013 - 3:27 AM

Vista WD4J - Vista para subir una lista de archivos, permitiendo la descarga y eliminación de los mismos

Vista WD4J - Vista para subir una lista de archivos, permitiendo la descarga y eliminación de los mismos

// Elementos del contexto en tiempo de diseño
/**
 *  - 0 Context
 *    - o File  1..1
 *    |     # FileData  TYPE  binary
 *    |     # FileName  TYPE  string
 *    - o IDList  0..N
 *    |     # UID       TYPE  string
 *    - o ScreenConfig  1..1
 *          # topDynamicNodeName  TYPE  string
 **/
 
// Elementos de la vista en tiempo de diseño
/**
 *  - 0 RootUIElementContainer
 *    | # Layout  TYPE GridLayout
 *    | # colCount  CONSTANT  2 
 *    - o FileUpload  TYPE FileUpload
 *    |     # data  BIND  File.FileData
 *    |     # fileName  BIND  File.FileName
 *    - o UploadButton  TYPE  Button
 *    |     # text  CONSTANT  Subir
 *    |     # onAction  CONSTANT  FileUpload
 *    - o DeleteButton  TYPE  Button
 *    |     # text  CONSTANT  Eliminar
 *    |     # onAction  CONSTANT  deleteSelectedFiles
 *    - o FilelistTransparentContainer  TYPE  TransparentContainer
 *    |     # layout  TYPE GridLayout
 *    |     # ColCount  CONSTANT 1
 **/

//@@begin javadoc:wdDoInit()
/** Hook method called to initialize controller. */
//@@end
public void wdDoInit()
{
//@@begin wdDoInit()
  // Cambiar el tipo de datos del atributo del contexto "File.FileData"
  // para darle compatibilidad con el elemento UI FileUpload
  wdContext.nodeFile().getNodeInfo().getAttribute
    (IPrivateFileDownloadDynamicView.IFileElement.FILE_DATA).getModifiableSimpleType();
  // Se crea de forma dinámica un nodo de contexto, el nombre queda almacenado
  // en wdContext.currentSreenConfigElement().TOP_DYNAMIC_NODE_NAME
  wdThis.createFilesTopNode();
//@@end
}

//@@begin javadoc:createFilesTopNode()
/** Declared method. */
//@@end
public void createFilesTopNode( )
{
  //@@begin createFilesTopNode()
  // ID del nodo TOP para los CheckBox
	wdContext.currentConfigElement()
	  .setTopCheckBoxListNodeName( "CheckBoxListComponentNode" );
	//	Se crea de forma dinámica el nodo de contexto
	wdContext.getNodeInfo().addChild(
		wdContext.currentConfigElement().getTopCheckBoxListNodeName(),
		null,
		true,
		true,
		false,
		false,
		false,
		true,
		null,
		null,
		null
	);
	// ID del nodo TOP para los Archivos
	wdContext.currentConfigElement()
	  .setTopFileListNodeName( "FileListComponentNode" );
	//	Se crea de forma dinámica el nodo de contexto
	wdContext.getNodeInfo().addChild(
		wdContext.currentConfigElement().getTopFileListNodeName(),
		null,
		true,
		true,
		false,
		false,
		false,
		true,
		null,
		null,
		null
	);
  //@@end
}

//@@begin javadoc:createFileDownloadCheckBox()
/** Declared method. */
//@@end
public void createFileDownloadCheckBox( )
{
  //@@begin createFileDownloadCheckBox()
  if ( wdContext.currentFileElement().getFileName() == null ||
		 wdContext.currentFileElement().getFileData() == null ){
	  // TODO Puede que se desee enviar un mensaje al usuario
	  return;
	}
	// Referencia a la vista
	IWDView view = (IWDView)wdControllerAPI;
	// Referencia al TransparentContainer en donde se agregara el nuevo elemento
	IWDTransparentContainer fileListTC =
	  (IWDTransparentContainer)view.getElement( "FilelistTransparentContainer" );
	// ID único para los nuevos elementos
	String uID = "Attr" + new Integer(wdContext.currentScreenConfigElement().getCheckBoxCount()).toString();
	wdContext.currentScreenConfigElement().setCheckBoxCount( 
		wdContext.currentScreenConfigElement().getCheckBoxCount() + 1 );
	// -- 
	// Nuevo FileDownload UI Element
	IWDFileDownload fileDownload =
	  (IWDFileDownload) view.createElement( IWDFileDownload.class, uID + "FileDownload" );
	fileDownload.setText( wdContext.currentFileElement().getFileName() );
	// Crear atributo de contexto para el archivo
	wdContext.getNodeInfo().getChild(
	  wdContext.currentScreenConfigElement().getTopFileListNodeName() )
	  					.addAttribute( uID, "ddic:com.sap.dictionary.binary" );
	wdContext.getChildNode( wdContext.currentScreenConfigElement().getTopFileListNodeName(), 0 )
		.getCurrentElement().setAttributeValue( uID, wdContext.currentFileElement().getFileData() );
	// Crear Binding entre el dato y el nuevo atributo de contexto
	fileDownload.bindData( wdContext.currentScreenConfigElement().getTopFileListNodeName() + "." + uID );
	// Cambiar el tipo de datos del atributo creado para darle
	// compatibilidad con el elemento UI FileDownload
	IWDModifiableBinaryType bt = (IWDModifiableBinaryType)
		wdContext.getChildNode( wdContext.currentScreenConfigElement()
			.getTopFileListNodeName(), 0 ).getNodeInfo().getAttribute(uID).getModifiableSimpleType();
	// Establecer el nombre del archivo
	bt.setFileName( wdContext.currentFileElement().getFileName() );
	// --
	// Nuevo TransparentContainer
	IWDTransparentContainer transparentContainer = (IWDTransparentContainer)
	view.createElement( IWDTransparentContainer.class, uID + "TransparentContainer" );
	// -- 
	// Nuevo CheckBox
	IWDCheckBox checkBox = (IWDCheckBox)view.createElement( IWDCheckBox.class, uID );
	// Crear atributo de contexto para el checkBox
	wdContext.getNodeInfo().getChild( 
	  wdContext.currentScreenConfigElement().getTopCheckBoxListNodeName() )
	  							.addAttribute( uID, "ddic:com.sap.dictionary.boolean" );
	// Crear Binding entre el checkBox y el nuevo atributo de contexto
	checkBox.bindChecked( wdContext.currentScreenConfigElement().getTopCheckBoxListNodeName() + "." + uID );
	// -- 
	// Agregar elementos al nuevo TransparentContainer
	transparentContainer.addChild( checkBox );
	transparentContainer.addChild( fileDownload );
	// Añadir al Contenedor de archivos
	fileListTC.addChild( transparentContainer );
	// cuando todo salga bien se agrega el ID del atributo creado a la lista de atributos
	IIDListElement newID = wdContext.createIDListElement();
	newID.setUID( uID );
	wdContext.nodeIDList().addElement( newID );
  //@@end
}

//@@begin javadoc:clearFileContextNode()
/** Declared method. */
//@@end
public void clearFileContextNode( )
{
  //@@begin clearFileContextNode()
  wdContext.currentFileElement().setFileData( null );
  wdContext.currentFileElement().setFileName( null );
  //@@end
}

//@@begin javadoc:deleteSelectedFiles()
/** Declared method. */
//@@end
public void deleteSelectedFiles( )
{
  //@@begin deleteSelectedFiles()
  // Nodo dinámico
	IWDNode node = wdContext.currentContextElement().node().getChildNode(
						wdContext.currentScreenConfigElement().getTopCheckBoxListNodeName(), 0);
	IWDNodeElement nodeElement = node.getCurrentElement();
	IIDListNode iDListnode = wdContext.nodeIDList();
	// Se deben recorrer los atributos en busca de los seleccionados
	if ( !iDListnode.isEmpty() ){
	int index = 0;
	while ( index < iDListnode.size() ){
		if (((Boolean)nodeElement.getAttributeValue(
			  iDListnode.getIDListElementAt(index).getUID())).booleanValue()){
		  wdThis.removeFileDownloadCheckBox(iDListnode.getIDListElementAt(index).getUID());
		  iDListnode.removeElement(iDListnode.getIDListElementAt(index));
		} else {
		  index++;
		}
	  }
	}
  //@@end
}

//@@begin javadoc:removeFileDownloadCheckBox()
/** Declared method. */
/* Elimina un elemento de nombre indicado por uID */
//@@end
public void removeFileDownloadCheckBox( java.lang.String uID )
{
  //@@begin removeFileDownloadCheckBox()
  if ( uID.equalsIgnoreCase("") ){
	  // Nada para eliminar
	  return;
	}
	// Referencia a la vista
	IWDView view = (IWDView)wdControllerAPI;
	// Referencia al TransparentContainer en donde se encuentra el elemento a eliminar
	IWDTransparentContainer fileListTC =
	  (IWDTransparentContainer)view.getElement( "FilelistTransparentContainer" );
	// Eliminar elemento seleccionado
	fileListTC.removeChild( uID + "TransparentContainer" );
  //@@end
}


/** ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * Acciones correspondientes a los botones que posee la vista
 * "Subir" archivo y "Eliminar" archivos seleccionados.
 * 
 * 
 * */
 
//@@begin javadoc:onActionFileUpload(ServerEvent)
/** Declared validating event handler. */
//@@end
public void onActionFileUpload(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
  //@@begin onActionFileUpload(ServerEvent)
  wdThis.createFileDownloadCheckBox();
	wdThis.clearFileContextNode();
  //@@end
}

//@@begin javadoc:onActiondeleteSelectedFiles(ServerEvent)
/** Declared validating event handler. */
//@@end
public void onActiondeleteSelectedFiles(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
{
  //@@begin onActiondeleteSelectedFiles(ServerEvent)
  // Se deben eliminar los archivos seleccionados
  wdThis.deleteSelectedFiles();
  //@@end
}