Changeset 3972


Ignore:
Timestamp:
Nov 15, 2007, 12:49:51 PM (14 years ago)
Author:
Nicklas Nordborg
Message:

References #492: Web services

Added ServicesUtil?.listToInfo() method for generic conversion of items to info objects

Location:
trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/BioAssaySet.java

    r3956 r3972  
    347347    @since 2.5
    348348   */
    349   public BioAssaySetInfo toInfo(BioAssaySetInfo info, DbControl dc)
     349  public BioAssaySetInfo toInfo(BioAssaySetInfo info)
    350350  {
    351351    super.toInfo(info);   
     
    354354    info.setNumReporters(this.getNumReporters());
    355355    info.setNumSpots(this.getNumSpots());
    356    
    357     ItemQuery<BioAssay> query = this.getBioAssays();   
    358     List<BioAssay> bioas = null;
    359     int[] basIds;;
    360     try
    361     {
    362       bioas = query.list(dc);
    363       basIds = new int[bioas.size()];
    364     }
    365     catch (PermissionDeniedException pex)
    366     {
    367       basIds = new int[0];
    368     }     
    369     for (int i=0; i<basIds.length; i++)
    370     {
    371       basIds[i] = bioas.get(i).getId();
    372     }   
    373     info.setBioAssayIds(basIds);
    374    
    375356    return info;
    376357  }
  • trunk/src/info/net/sf/basedb/info/BioAssaySetInfo.java

    r3959 r3972  
    3737public class BioAssaySetInfo
    3838  extends BasicItemInfo
    39     implements NameableInfo, Serializable
     39  implements NameableInfo, Serializable
    4040{
    4141  private static final long serialVersionUID = -1192040975292178464L;
     
    4444  private int numReporters = 0;
    4545  private int numSpots = 0;
    46   private int[] bioAssayIds;
    4746 
    4847  /**
     
    8685  }
    8786
    88   /**
    89       @param bioAssayIds The bioAssayIds to set.
    90   */
    91   public void setBioAssayIds(int[] bioAssayIds)
    92   {
    93     this.bioAssayIds = bioAssayIds;
    94   }
    95 
    96   /**
    97     @return Returns the bioAssayIds.
    98   */
    99   public int[] getBioAssayIds()
    100   {
    101     return bioAssayIds;
    102   }
    103 
    10487  public String getDescription()
    10588  {   
  • trunk/src/test/TestWebservices.java

    r3964 r3972  
    292292        if (hasDownloadableData && fileTypes.length > 0)
    293293        {
    294           System.out.print("--Download of array design file....");
     294          System.out.print("--Download of " + fileTypes[0].getName() + " for array design....");
    295295          File receivedFile = new File(fileTypes[0].getName());
    296296          OutputStream out = new FileOutputStream(receivedFile);
     
    373373      if (hasDownloadableData && fileTypes.length > 0)
    374374      {
    375         System.out.print("--Download of raw data file for RawBioAssay....");
     375        System.out.print("--Download of " + fileTypes[0].getName() + " for RawBioAssay....");
    376376        File receivedFile = new File(fileTypes[0].getName());
    377377        OutputStream out = new FileOutputStream(receivedFile);
  • trunk/src/webservices/server/net/sf/basedb/ws/server/ArrayDesignService.java

    r3964 r3972  
    3434import net.sf.basedb.info.DataFileTypeInfo;
    3535import net.sf.basedb.info.QueryOptions;
    36 
    37 import java.util.LinkedList;
    38 import java.util.List;
    3936
    4037import org.apache.axiom.om.OMElement;
     
    7471    {
    7572      ArrayDesign design = ArrayDesign.getById(dc, arrayDesignId);
    76       DataFileType type = DataFileType.getByExternalId(dc, dataFileType);
    7773      if (design.hasFileSet())
    7874      {
    7975        FileSet fileSet = design.getFileSet();
     76        DataFileType type = DataFileType.getByExternalId(dc, dataFileType);
    8077        if (fileSet.hasMember(type))
    8178        {
     
    128125    SessionControl sc = getSessionControl(ID);
    129126    DbControl dc = sc.newDbControl();
    130     List<DataFileTypeInfo> types = new LinkedList<DataFileTypeInfo>();
    131127   
    132128    try
     
    136132      {
    137133        FileSet fileSet = design.getFileSet();
    138         ItemQuery<FileSetMember> query = fileSet.getMembers();       
    139         if (qOpt != null) util.getConfiguredItemQuery(query, qOpt);
    140         for (FileSetMember fsm : query.list(dc))
    141         {
    142           DataFileTypeInfo dftInfo = new DataFileTypeInfo();
    143           dftInfo = fsm.getDataFileType().toInfo(dftInfo);
    144           types.add(dftInfo);
    145         }
     134        ItemQuery<DataFileType> query = fileSet.getMemberTypes();       
     135        query = util.getConfiguredItemQuery(query, qOpt);
     136        return util.listToInfo(query.list(dc), DataFileTypeInfo.class);
    146137      }
    147138    }
     
    150141      if (dc != null) dc.close();
    151142    }
    152     DataFileTypeInfo[] dataFileTypeInfos = new DataFileTypeInfo[types.size()];
    153     return types.toArray(dataFileTypeInfos);
     143    return new DataFileTypeInfo[0];
    154144  }
    155145}
  • trunk/src/webservices/server/net/sf/basedb/ws/server/ExperimentService.java

    r3956 r3972  
    6969      ItemQuery<Experiment> query= Experiment.getQuery();   
    7070      query = util.getConfiguredItemQuery(query, qOpt);
    71      
    72       for (Experiment experiment : query.list(dc))
    73       {
    74         ExperimentInfo expInfo = new ExperimentInfo();
    75         experiment.toInfo(expInfo);
    76         experiments.add(expInfo);
    77       }
     71      return util.listToInfo(query.list(dc), ExperimentInfo.class);
    7872    }
    7973    finally
     
    8175      if (dc != null) dc.close();
    8276    }
    83     ExperimentInfo[] ii = new ExperimentInfo[experiments.size()];   
    84     return experiments.toArray(ii);
    8577  }
    8678 
     
    9587  public BioAssaySetInfo[] getBioAssaySets(String ID, int experimentId, QueryOptions qOpt)
    9688  {
    97     List<BioAssaySetInfo> bioAssaySetsInfo = new LinkedList<BioAssaySetInfo>();
    9889    SessionControl sc = getSessionControl(ID);
    9990    DbControl dc = sc.newDbControl();
    100    
    10191    try
    10292    {
    10393      Experiment experiment = Experiment.getById(dc, experimentId);
    10494      ItemQuery<BioAssaySet> query = experiment.getBioAssaySets();
    105       query = qOpt != null ? util.getConfiguredItemQuery(query, qOpt) : query;
    106      
    107       for (BioAssaySet bas : query.list(dc))
    108       {
    109         BioAssaySetInfo basInfo = new BioAssaySetInfo();
    110         bas.toInfo(basInfo, dc);       
    111         bioAssaySetsInfo.add(basInfo);
    112       }
     95      query = util.getConfiguredItemQuery(query, qOpt);
     96      return util.listToInfo(query.list(dc), BioAssaySetInfo.class);
    11397    }
    11498    finally
     
    116100      if (dc != null) dc.close();
    117101    }
    118    
    119     BioAssaySetInfo[] ii = new BioAssaySetInfo[bioAssaySetsInfo.size()];
    120     return bioAssaySetsInfo.toArray(ii);
    121102  }
    122103 
     
    133114    SessionControl sc = getSessionControl(ID);
    134115    DbControl dc = sc.newDbControl();
    135     List<RawBioAssayInfo> rawBioAssays = new LinkedList<RawBioAssayInfo>();
    136116    try
    137117    {
    138118      Experiment experiment = Experiment.getById(dc, experimentId);
    139119      ItemQuery<RawBioAssay> query = experiment.getRawBioAssays();     
    140       query = qOpt != null ? util.getConfiguredItemQuery(query, qOpt) : query;
    141      
    142       for (RawBioAssay rba : query.list(dc))
    143       {       
    144         RawBioAssayInfo rbaInfo = new RawBioAssayInfo();
    145         rbaInfo = rba.toInfo(rbaInfo);
    146         rawBioAssays.add(rbaInfo);
    147       }
    148      
     120      query = util.getConfiguredItemQuery(query, qOpt);
     121      return util.listToInfo(query.list(dc), RawBioAssayInfo.class);
    149122    }
    150123    finally
     
    152125      if (dc != null) dc.close();
    153126    }
    154     RawBioAssayInfo[] ii = new RawBioAssayInfo[rawBioAssays.size()];
    155     return rawBioAssays.toArray(ii);
    156127  }
    157128}
  • trunk/src/webservices/server/net/sf/basedb/ws/server/ProjectService.java

    r3956 r3972  
    3131import net.sf.basedb.info.QueryOptions;
    3232
    33 import java.util.LinkedList;
    34 import java.util.List;
    3533
    3634/**
     
    8381  {
    8482    SessionControl sc = getSessionControl(ID);
    85     DbControl dc = sc.newDbControl();
    86     List<ProjectInfo> projects = new LinkedList<ProjectInfo>();   
     83    DbControl dc = sc.newDbControl();
    8784    try
    8885    {
    8986      ItemQuery<Project> query = Project.getQuery();
    90       query = qOpt != null ? util.getConfiguredItemQuery(query, qOpt) : query;
    91      
    92       for (Project project : query.list(dc))
    93       {
    94         ProjectInfo pInfo = new ProjectInfo();
    95         project.toInfo(pInfo);       
    96         projects.add(pInfo);
    97       }
     87      query = util.getConfiguredItemQuery(query, qOpt);
     88      return util.listToInfo(query.list(dc), ProjectInfo.class);
    9889    }
    9990    finally
     
    10192      if (dc != null) dc.close();
    10293    }
    103     ProjectInfo[] ii = new ProjectInfo[projects.size()];
    104     return projects.toArray(ii);
    10594  }
    10695}
  • trunk/src/webservices/server/net/sf/basedb/ws/server/RawBioAssayService.java

    r3964 r3972  
    3737import net.sf.basedb.info.QueryOptions;
    3838
    39 import java.util.LinkedList;
    40 import java.util.List;
    4139
    4240import org.apache.axiom.om.OMElement;
     
    6765    SessionControl sc = getSessionControl(ID);
    6866    DbControl dc = sc.newDbControl();
    69     ArrayDesign arrayDesign = null;
     67    ArrayDesignInfo info = null;
    7068    try
    7169    {
    7270      RawBioAssay rawBioAssay = RawBioAssay.getById(dc, rawBioAssayId);
    73       arrayDesign= rawBioAssay.getArrayDesign();
     71      ArrayDesign arrayDesign = rawBioAssay.getArrayDesign();
     72      if (arrayDesign != null) info = arrayDesign.toInfo(new ArrayDesignInfo());
    7473    }
    7574    finally
     
    7776      if (dc != null) dc.close();
    7877    }
    79    
    80     ArrayDesignInfo adInfo = new ArrayDesignInfo();
    81     arrayDesign.toInfo(adInfo);
    82     return adInfo;
     78    return info;
    8379  }
    8480 
     
    10197    {
    10298      RawBioAssay rawBioAssay = RawBioAssay.getById(dc, rawBioAssayId);
    103       DataFileType type = DataFileType.getByExternalId(dc, dataFileType);
    10499      if (rawBioAssay.hasFileSet())
    105100      {
    106101        FileSet fileSet = rawBioAssay.getFileSet();
     102        DataFileType type = DataFileType.getByExternalId(dc, dataFileType);
    107103        if (fileSet.hasMember(type))
    108104        {
     
    156152    SessionControl sc = getSessionControl(ID);
    157153    DbControl dc = sc.newDbControl();
    158     List<DataFileTypeInfo> types = new LinkedList<DataFileTypeInfo>();
    159154    try
    160155    {
     
    162157      if (rawBioAssay.hasFileSet())
    163158      {     
    164         ItemQuery<FileSetMember> query = rawBioAssay.getFileSet().getMembers();
    165         if (qOpt != null) util.getConfiguredItemQuery(query, qOpt);
    166         for (FileSetMember fsm : query.list(dc))
    167         {
    168           DataFileTypeInfo dftInfo = new DataFileTypeInfo();
    169           fsm.getDataFileType().toInfo(dftInfo);
    170           types.add(dftInfo);
    171         }
     159        ItemQuery<DataFileType> query = rawBioAssay.getFileSet().getMemberTypes();
     160        query = util.getConfiguredItemQuery(query, qOpt);
     161        return util.listToInfo(query.list(dc), DataFileTypeInfo.class);
    172162      }
    173163    }
     
    176166      if (dc != null) dc.close();
    177167    }
    178     DataFileTypeInfo[] dataFileTypeInfo = new DataFileTypeInfo[types.size()];
    179     return types.toArray(dataFileTypeInfo);
     168    return new DataFileTypeInfo[0];
    180169  }
    181170}
  • trunk/src/webservices/server/net/sf/basedb/ws/server/ServicesUtil.java

    r3952 r3972  
    2828import net.sf.basedb.core.ItemQuery;
    2929import net.sf.basedb.core.query.Hql;
     30import net.sf.basedb.info.BasicItemInfo;
    3031import net.sf.basedb.info.QueryOptions;
    3132
    32 import java.util.ArrayList;
     33import java.lang.reflect.InvocationTargetException;
     34import java.lang.reflect.Method;
     35import java.util.EnumSet;
    3336import java.util.List;
     37import java.util.Set;
    3438
    3539/**
    3640  A utility class for common methods in the web services.
    37     @author Martin
     41    @author Martin, Nicklas
    3842    @version 2.5
    3943 */
     
    5054   */
    5155  @SuppressWarnings("unchecked")
    52   protected <T extends BasicItem> ItemQuery<T> getConfiguredItemQuery(ItemQuery<T> query, QueryOptions qOpt)
     56  public <T extends BasicItem> ItemQuery<T> getConfiguredItemQuery(ItemQuery<T> query, QueryOptions qOpt)
    5357  {   
    54     List<Include> includes = new ArrayList<Include>();
    55     qOpt = qOpt == null ? new QueryOptions() : qOpt;
     58    Set<Include> includes = EnumSet.noneOf(Include.class);
     59    if (qOpt == null) qOpt = new QueryOptions();
    5660    if (qOpt.getIncludeMine()) includes.add(Include.MINE);
    5761    if (qOpt.getIncludeShared()) includes.add(Include.SHARED);
     
    6266    query.include(includes);
    6367    if (qOpt.getRestriction() != null)
    64     { 
    65       query.restrict(Hql.restriction(qOpt.getRestriction(), qOpt.getRestrictionPrefix()));     
    66     }   
     68    {
     69      query.restrict(Hql.restriction(qOpt.getRestriction(), qOpt.getRestrictionPrefix()));
     70    }
    6771    return query;
    6872  }
     73 
     74  /**
     75    Convert a list of objects to an array of info objects
     76    objects. This method uses reflection to find the correct method. For this
     77    to work there are a few requirements:
     78   
     79    <ul>
     80    <li>The list must contain objects of the same class only.
     81    <li>The class for the objects in the list must contain a method
     82      with the name "toInfo".
     83    <li>The "toInfo" method must take an object of the specified
     84      info class as the single parameter.
     85    <li>The "toInfo" method must return the same object that was
     86      passed to it.
     87    </ul>
     88   
     89    See for example: {@link net.sf.basedb.core.Experiment#toInfo(net.sf.basedb.info.ExperimentInfo)}
     90   
     91    <p>
     92    Note that this method is not limited to BasicItem object. It will work with
     93    any kind of object as long as the above requirements are fulfilled.
     94   
     95    @param list A list of BasicItem objects or one of the subclasses
     96    @param clazz The class of the BasicItemInfo that is matching the BasicItem:s
     97      in the list
     98    @return An array info objects of the specified class
     99  */
     100  @SuppressWarnings("unchecked")
     101  public <T> T[] listToInfo(List<?> list, Class<T> clazz)
     102  {
     103    T[] result = (T[])java.lang.reflect.Array.newInstance(clazz, list.size());
     104    int i = 0;
     105    Method toInfo = null;
     106    try
     107    {
     108      for (Object o : list)
     109      {
     110        if (toInfo == null) toInfo = o.getClass().getDeclaredMethod("toInfo", clazz);
     111        result[i] = clazz.newInstance();
     112        toInfo.invoke(o, result[i]);
     113        ++i;
     114      }
     115    }
     116    catch (NoSuchMethodException ex)
     117    {
     118      throw new RuntimeException(ex);
     119    }
     120    catch (InstantiationException ex)
     121    {
     122      throw new RuntimeException(ex);
     123    }
     124    catch (IllegalAccessException ex)
     125    {
     126      throw new RuntimeException(ex);
     127    }
     128    catch (InvocationTargetException ex)
     129    {
     130      throw new RuntimeException(ex);
     131    }
     132    return result;
     133  }
     134 
    69135}
Note: See TracChangeset for help on using the changeset viewer.