Commit 341a4a2f authored by Guanji Wang's avatar Guanji Wang
Browse files

the new version of download functionality of java source code in fr.cnrs.sitools.extensions

parent 11e38f73
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -100,15 +100,15 @@ public class DirectOrderResource extends OrderResource {
    
    if ("zip".equals(archiveType)) {
      repr = new ZipOutputRepresentation(listReferences.getReferencesSource(), getClientInfo(), getContext(), fileName
          + ".zip");
          + ".zip",listReferences.getfilename_hash());
    }
    else if ("tar.gz".equals(archiveType)) {
      repr = new TarOutputRepresentation(listReferences.getReferencesSource(), getClientInfo(), getContext(), fileName
          + ".tar.gz", true);
          + ".tar.gz", true,listReferences.getfilename_hash());
    }
    else if ("tar".equals(archiveType)) {
      repr = new TarOutputRepresentation(listReferences.getReferencesSource(), getClientInfo(), getContext(), fileName
          + ".tar", false);
          + ".tar", false,listReferences.getfilename_hash());
    }
    else {
      getResponse().setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
+27 −0
Original line number Diff line number Diff line
package fr.cnes.sitools.resources.order;



public class FileOrderResource {
  private String file_name_gj;
  private String file_path_gj;
  public FileOrderResource(String filename, String filepath) {
    // TODO Auto-generated constructor stub
    this.file_name_gj = filename;
    this.file_path_gj = filepath;
  }
  void setfilename(String fn){
    file_name_gj = fn ;
  }
  String getFileName()
  {
    return file_name_gj;
  }
  void setfilepath(String fp){
    file_path_gj = fp ;
  }
  String getFilePath()
  {
    return file_path_gj;
  }   
}
+176 −18
Original line number Diff line number Diff line
@@ -18,11 +18,16 @@
 ******************************************************************************/
package fr.cnes.sitools.resources.order;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.restlet.data.Reference;
import org.restlet.data.Status;
@@ -51,13 +56,49 @@ import fr.cnes.sitools.util.RIAPUtils;
 * 
 * @author m.gond 
 */ 

public class OrderResource extends AbstractDatasetOrderResource {
  /** Maximum number of file to download authorized, default to -1 => no limit */
  private int nbMaxDownload = -1;
  private HashMap<String, String> mapTypeFile = new HashMap<String, String>();
//  private HashMap<String, String> mapTypeFile2 = new HashMap<String, String>();
  private static final Logger LOG = Logger.getLogger(OrderResource.class.getName());

  @Override
  public ListReferencesAPI listFilesToOrder(DatabaseRequest dbRequest) throws SitoolsException {
    
    /*
    mapTypeFile.put("aia.lev1", "image_lev1.fits");
    mapTypeFile.put("hmi.m_720s", "magnetogram.fits");
    mapTypeFile.put("hmi.Ic_720s", "continuum.fits");
    mapTypeFile.put("hmi.sharp_720s_nrt", "continuum.fits");
    mapTypeFile.put("hmi.sharp_720s_nrt", "info_map.fits");
    mapTypeFile.put("hmi.sharp_720s_nrt", "confid_map.fits");
        
    mapTypeFile2.put("aia.lev1", "/S00000/image_lev1.fits");
    mapTypeFile2.put("hmi.m_720s", "magnetogram.fits");
    mapTypeFile2.put("hmi.Ic_720s", "continuum.fits");
    
    mapTypeFile2.put("hmi.sharp_720s_nrt", "continuum.fits");
    mapTypeFile2.put("hmi.sharp_720s_nrt", "info_map.fits");
    mapTypeFile2.put("hmi.sharp_720s_nrt", "confid_map.fits");
    */
   // mapTypeFile.put("hmi.m_720s", "magnetogram.fits");
    task.setCustomStatus("Creating list of files to order");
    // save the series_name into different arrays
    /*
    String[] arr_hmi_sharp_720s_nrt = {
     "vlos_mag.fits", "vlos_err.fits",           "src_grad.fits",    "src_continuum.fits",    "magnetogram.fits", 
     "info_map.fits", "inclin_azimuth_err.fits", "inclination.fits", "inclination_err.fits", "inclination_alpha_err.fits", 
     "field_inclination_err.fits", "field.fits", "field_err.fits",   "field_az_err.fits",     "field_alpha_err.fits",
      "eta_0.fits",    "dop_width.fits",         "Dopplergram.fits",  "disambig.fits",           "damping.fits",
     "conv_flag.fits",  "continuum.fits",         "confid_map.fits",   "chisq.fits",            "bitmap.fits",
     "azimuth.fits",   "azimuth_err.fits",   "azimuth_alpha_err.fits", "alpha_mag.fits",       "alpha_err.fits",
     "conf_disambig.fits" };
    String[] arr_hmi_m_720s = {"magnetogram.fits"};
    
 */   
    
    ResourceModel resourceModel = getModel();
    ResourceParameter nbMaxDownloadParam = resourceModel.getParameterByName("too_many_selected_threshold");
    if (nbMaxDownloadParam != null && !"".equals(nbMaxDownloadParam)) {
@@ -77,32 +118,114 @@ public class OrderResource extends AbstractDatasetOrderResource {
    }

    ResourceParameter colUrl = resourceModel.getParameterByName("colUrl");
    
    if (colUrl.getValue() == null || colUrl.getValue().equals("")) {
      throw new SitoolsException("NO COLUMN DEFINED FOR ORDER");
    }

    ListReferencesAPI list = new ListReferencesAPI(settings.getPublicHostDomain() + settings.getString(Consts.APP_URL));
    

    
    while (dbRequest.nextResult()) {
      
      // create a hashmap with one key value and one string array which type is an object called 'FileOrderResource' created 
      // by guanji in package 'fr.cnes.sitools.resources.order'
 //     Map<String, List<FileOrderResource>> mapbyseries = new HashMap<String, List<FileOrderResource>>();
      
      Record rec = dbRequest.getRecord();
    //***********************************************************************************
      List<AttributeValue> listAttributes = rec.getAttributeValues(); 
      // one rec = one line of data and one line of data contains one or more fits files
      AttributeValue obj;
      String sunum = null;
     // String filename=null; 
     
      String wave=null;
      String date=null;
      String series_name=null;;
      
      // Get the file path from the colUrl of the chosen line of record
      AttributeValue attributeValue = OrderResourceUtils.getInParam(colUrl, rec);

      if (attributeValue != null && attributeValue.getValue() != null) {
        // get the file path
      String filePath = (String) attributeValue.getValue();
      
    //  List<String> filename_list = new ArrayList<String>();
    //  List<String> filepath_list = new ArrayList<String>();
      List<FileOrderResource> listname_path = new ArrayList<FileOrderResource>();
      
       
      for (Iterator<AttributeValue> it = listAttributes.iterator(); it.hasNext();) {
        obj = it.next();
        if (obj.getName().equals("sunum")) {
          sunum = obj.getValue().toString();
        }
        if (obj.getName().equals("wavelnth")) {
          wave = obj.getValue().toString();
        }
        if (obj.getName().equals("date__obs")) {
          date = obj.getValue().toString();
        }
        if (obj.getName().equals("series_name")) {
          series_name = obj.getValue().toString();
        }
   //     String filename=series_name+"."+wave+"A_"+date+".";      
       }    
      String filename=series_name+"."+wave+"A_"+date+"."; 
      
      /*
      if(series_name.equals("hmi.sharp_720s_nrt"))
      {
        for(int i=0; i<arr_hmi_sharp_720s_nrt.length; i++)
        {
         String filename2 = filename + arr_hmi_sharp_720s_nrt[i];
         String filePath2 = filePath + "/" + arr_hmi_sharp_720s_nrt[i];
         listname_path.add(new FileOrderResource(filename2, filePath2));
         mapbyseries.put(series_name, listname_path);  
         System.out.println("series_name is " + series_name + " is chosen correctly. And the filename is:  " +filename2 +" " +"The filepath equals : " + filePath2  + "\n");
         System.out.println("the list size equals : " + listname_path.size() + "\n");    
        }
      }  
      if(series_name.equals("hmi.m_720s") )
      {     
        for(int i=0; i<arr_hmi_m_720s.length; i++)
        {
          String filename2 = filename + arr_hmi_m_720s[i];                        
          String filePath2 = filePath +"/" + arr_hmi_m_720s[i];
          listname_path.add(new FileOrderResource(filename2, filePath2));
          mapbyseries.put(series_name, listname_path);  
          System.out.println("series_name is " + series_name + " is chosen correctly. And the filename is:  " +filename2 +" " +"The filepath equals : " + filePath2  + "\n");
          System.out.println("the list size equals : " + listname_path.size() + "\n");  
        }    
      } 
      else
      {
        System.out.println("Attention!!!" +"\n" + "Guanji debug Info: We can't find the correct series name !!!!" + "\n"); 
        System.out.println("series_name is " + series_name + "\n");
      }
    */
     
      
      if (attributeValue != null && attributeValue.getValue() != null) 
      {    
        String urlAttach = settings.getString(Consts.APP_DATASTORAGE_URL);
        // if it contains "/datastorage" get rid of everything before
        if (filePath.contains(urlAttach)) {
          filePath = filePath.substring(filePath.lastIndexOf(urlAttach));
        listname_path = get_file_list(filePath,series_name,filename,urlAttach);
        for(FileOrderResource file_resource : listname_path){
          String f_path = file_resource.getFilePath(); // get the 2nd element of an object array list
          if(f_path.contains(urlAttach)){
            file_resource.setfilepath(f_path.substring(f_path.lastIndexOf(urlAttach)));
          }
        if (filePath.startsWith("http://")) {
          list.addReferenceSource(new Reference(filePath));
          if(f_path.startsWith("http://")){
            list.addReferenceSource(new Reference(f_path));
          }
          else{
          list.addReferenceSource(new Reference(RIAPUtils.getRiapBase() + filePath));
            list.addReferenceSource(new Reference(RIAPUtils.getRiapBase() + f_path));
          }
          list.addfilename(sunum,file_resource.getFileName());
        }

      }
      // end of if attributeValue != null         
    }
    // end of loop for records of seleted all lines
    return list;
  }

@@ -195,8 +318,43 @@ public class OrderResource extends AbstractDatasetOrderResource {
      throw new SitoolsException("Error while creating the file index in the userstorage", e);
    }
    return null;
  }
  
  // List all files in a recursive directory by guanji wang  
  public List<FileOrderResource> get_file_list(String directoryName, String series_name, String filename,  String urlAttach){
    String directoryName2 = null;
    if(directoryName.contains(urlAttach)){
      directoryName2 = directoryName.substring(directoryName.lastIndexOf(urlAttach)).split("/datastorage/user")[1];
//    directoryName = RIAPUtils.getRiapBase() + directoryName;
    }
    File dir = new File(directoryName2);    
    File[] dir_list = dir.listFiles();
    
    List<FileOrderResource> listname_path = new ArrayList<FileOrderResource>();
    System.out.println("The director/Path of files is " +directoryName +" , please check your path" + "\n");
    if(dir_list != null )
    {
      for(File file : dir_list)
      {
        if (file.isFile()||file.getName().endsWith(".fits")){
          String filename2 = filename + file.getName();
          String filePath2 = directoryName + "/" + file.getName();
          listname_path.add(new FileOrderResource(filename2, filePath2));
          System.out.println(" GUANJI INFO : One " + series_name + " file is added, its filename : " + filename2 + " its file path : " + filePath2 + "\n" );
          System.out.println("The list_name_path size is : " + listname_path.size() + "\n");
        }
        else if(file.isDirectory()){
          System.out.println("the path " + file.isDirectory() + " is not a file but a path, the system will enter this directory to find other fits files. ");
          get_file_list(file.getAbsolutePath(), series_name, filename, urlAttach);
        }
      }
    }
    else{
      System.out.println("The director - " +directoryName +" has no files" + "\n");
    }
    return listname_path;
  }
  

  /**
   * Create the Zip from the listOfFilesToOrder
+1 −0
Original line number Diff line number Diff line
@@ -99,6 +99,7 @@ public class OrderResourceFacade extends SitoolsParameterizedResource implements
    }
    else {
      getContext().getAttributes().remove(TaskUtils.BODY_CONTENT);
      System.out.println("Guanji Info: The Representation body is null, please check the code in OrderResourceFacade. ");
    }
  }

+5 −5
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ public class OrderResourceModel extends TaskResourceModel {

    super();
    setClassAuthor("AKKA Technologies");
    setClassOwner("CNES");
    setClassOwner("GUANJI");
    setClassVersion("0.1");
    setName("OrderResourceModel");
    setDescription("Order resources associated to metadata and save it in user storage. (Can also create a ZIP, TAR or TAR.GZ 'on the fly')");
@@ -59,7 +59,7 @@ public class OrderResourceModel extends TaskResourceModel {

    setRunTypeAdministration(TaskRunTypeAdministration.TASK_DEFAULT_RUN_ASYNC);

    ResourceParameter paramColUrl = new ResourceParameter("colUrl", "Colum containing data url for order",
    ResourceParameter paramColUrl = new ResourceParameter("colUrl", "Colume containing data url for order",
        ResourceParameterType.PARAMETER_INTERN);
    /** Type de paramètre pour lister les colonnes du dataset */
    paramColUrl.setValueType("xs:dataset.columnAlias");
@@ -85,7 +85,7 @@ public class OrderResourceModel extends TaskResourceModel {
    ResourceParameter paramTooManySelectedThreshold = new ResourceParameter("too_many_selected_threshold",
        "Maximum number of files allowed to be downloaded (-1 to set no limit)", ResourceParameterType.PARAMETER_INTERN);
    paramTooManySelectedThreshold.setValueType("xs:integer");
    paramTooManySelectedThreshold.setValue("-1");
    paramTooManySelectedThreshold.setValue("-1");     // set the value default to the files downloaded limit

    ResourceParameter paramMaxWarningThresholdText = new ResourceParameter("max_warning_threshold_text",
        "Text to display to the user when Warning threshold is reached", ResourceParameterType.PARAMETER_USER_GUI);
@@ -101,7 +101,7 @@ public class OrderResourceModel extends TaskResourceModel {
    this.addParam(paramMaxWarningThresholdText);
    this.addParam(paramTooManySelectedThresholdText);

    this.getParameterByName("fileName").setValue("dataset_order_" + "${date:yyyy-MM-dd HH_mm_ss}");
    this.getParameterByName("fileName").setValue("IAS_dataset_order_" + "${date:yyyy-MM-dd HH_mm_ss}");

  }

@@ -117,7 +117,7 @@ public class OrderResourceModel extends TaskResourceModel {
        String value = param.getValue();
        if (value == null || value.equals("")) {
          ConstraintViolation constraint = new ConstraintViolation();
          constraint.setMessage("An attribute of the dataset must be choosen");
          constraint.setMessage("An attribute url of the dataset must be choosen");
          constraint.setLevel(ConstraintViolationLevel.CRITICAL);
          constraint.setValueName(param.getName());
          constraints.add(constraint);
Loading