Changeset 4356


Ignore:
Timestamp:
Oct 4, 2012, 3:55:26 PM (10 years ago)
Author:
Fredrik Levander
Message:

Refs #772. Fixed permission problem if user doesn't have write permission to file being imported using hits import.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/api/core/src/org/proteios/core/Annotator.java

    r4003 r4356  
    158158        return annotation;
    159159    }
     160   
     161    /**
     162     * Get the {@link Annotation} object of the specified annotation type. If
     163     * the annotation set doesn't contain an annotation of the specified type null i returned.
     164     *
     165     * @param annotationType The annotation type
     166     * @return An existing <code>Annotation</code> object or null
     167     * @throws BaseException If there is another error
     168 */
     169
     170public Annotation getExistingAnnotation(Annotatable item,
     171 AnnotationType annotationType)
     172 throws InvalidUseOfNullException
     173 {
     174     log
     175     .debug("Get annotation item:" + item + " annotationType" + annotationType
     176     .getName());
     177     if (annotationType == null) {
     178    throw new InvalidUseOfNullException("annotationType");
     179  }
     180     AnnotationSet set = getAnnotationSet(item);
     181     AnnotationSetData setData = set.getData();
     182     Annotation annotation = factory.getItem(Annotation.class, setData
     183     .getAnnotations().get(annotationType.getData()));
     184     return annotation;
     185 }
    160186               
    161187    /**
  • trunk/plugin/src/org/proteios/plugins/PeakListToHitNonGelPlugin.java

    r4269 r4356  
    2929
    3030import org.proteios.core.Annotation;
     31import org.proteios.core.AnnotationSet;
    3132import org.proteios.core.AnnotationType;
    3233import org.proteios.core.Annotator;
     
    4546import org.proteios.core.SessionControl;
    4647import org.proteios.core.Type;
     48import org.proteios.core.data.AnnotationSetData;
    4749import org.proteios.core.plugin.About;
    4850import org.proteios.core.plugin.AboutImpl;
     
    6971 * @author olle
    7072 */
    71 public class PeakListToHitNonGelPlugin
    72     extends AbstractPlugin
    73 {
     73public class PeakListToHitNonGelPlugin extends AbstractPlugin {
    7474  /**
    7575   * Logger used. Used to log specific events.
    7676   */
    7777  protected static final org.apache.log4j.Logger log = org.apache.log4j.LogManager
    78     .getLogger("org.proteios.io");
     78      .getLogger("org.proteios.io");
    7979  public static final String ENTER_EXPLICIT_LOCAL_SAMPLE_ID = "enterExplicitLocalSampleId";
    8080  public static final String GET_LOCAL_SAMPLE_ID_FROM_FILE_BIOMATERIAL = "getLocalSampleIdFromFileBioMaterial";
     
    8585  public static final String NO_FRACTION_ID = "noFractionId";
    8686
    87 
    8887  @Override
    8988  public void init(SessionControl sc, ParameterValues configuration,
    90       ParameterValues job)
    91       throws BaseException
    92   {
     89      ParameterValues job) throws BaseException {
    9390    super.init(sc, configuration, job);
    9491  }
    9592
    96 
    9793  public void run(Request request, Response response,
    98       ProgressReporter progress)
    99   {
     94      ProgressReporter progress) {
    10095    String command = request.getCommand();
    101     if (command.equals(Request.COMMAND_EXECUTE))
    102     {
     96    if (command.equals(Request.COMMAND_EXECUTE)) {
    10397      execute(request, response, progress);
    104     }
    105     else if (command.equals(Request.COMMAND_CONFIGURE_PLUGIN))
    106     {
     98    } else if (command.equals(Request.COMMAND_CONFIGURE_PLUGIN)) {
    10799      response.setContinue(Request.COMMAND_EXECUTE);
    108     }
    109     else
    110     {
     100    } else {
    111101      response.setError("Unknown command " + command, null);
    112102    }
    113103  }
    114104
    115 
    116105  private void execute(Request request, Response response,
    117       ProgressReporter progress)
    118   {
     106      ProgressReporter progress) {
    119107    // Directory d = (Directory) job.getValue("dir");
    120108    ArrayList<Throwable> err = new ArrayList<Throwable>(1);
    121109    String localSampleIdEntryMode = (String) job
    122       .getValue("localSampleIdEntryMode");
    123     log
    124       .debug("localSampleIdEntryMode = \"" + localSampleIdEntryMode + "\"");
     110        .getValue("localSampleIdEntryMode");
     111    log.debug("localSampleIdEntryMode = \"" + localSampleIdEntryMode + "\"");
    125112    String localSampleId = (String) job.getValue("localSampleId");
    126113    String fractionIdEntryMode = (String) job
    127       .getValue("fractionIdEntryMode");
     114        .getValue("fractionIdEntryMode");
    128115    log.debug("fractionIdEntryMode = \"" + fractionIdEntryMode + "\"");
    129116    String regexPattern = (String) job.getValue("regexPattern");
     
    135122    Project project = Project.getById(dc, sc.getActiveProjectId());
    136123    List<FileType> fts = PeakListFileValidationFactory
    137       .getPossibleFileTypes(dc);
     124        .getPossibleFileTypes(dc);
    138125    dc.reattachItem(f);
    139126    //
    140127    List<String> localSampleIdStringList = new ArrayList<String>();
    141128    Set<BioMaterial> fileBioMaterialSet = f.getBioMaterials();
    142     for (BioMaterial fileBioMaterial : fileBioMaterialSet)
    143     {
     129    for (BioMaterial fileBioMaterial : fileBioMaterialSet) {
    144130      String fileBioMaterialName = fileBioMaterial.getName();
    145131      String fileBioMaterialName2 = fileBioMaterial.getExternalId();
    146       if (fileBioMaterialName != null && !fileBioMaterialName.equals(""))
    147       {
     132      if (fileBioMaterialName != null && !fileBioMaterialName.equals("")) {
    148133        // Add name to local sample id string list
    149134        localSampleIdStringList.add(fileBioMaterialName);
    150       }
    151       else if (fileBioMaterialName2 != null && !fileBioMaterialName2
    152         .equals(""))
    153       {
     135      } else if (fileBioMaterialName2 != null
     136          && !fileBioMaterialName2.equals("")) {
    154137        // Add name to local sample id string list
    155138        localSampleIdStringList.add(fileBioMaterialName2);
     
    157140    }
    158141    if (localSampleIdEntryMode
    159       .equals(GET_LOCAL_SAMPLE_ID_FROM_FILE_BIOMATERIAL))
    160     {
    161       if (localSampleIdStringList != null && localSampleIdStringList
    162         .size() != 0)
    163       {
     142        .equals(GET_LOCAL_SAMPLE_ID_FROM_FILE_BIOMATERIAL)) {
     143      if (localSampleIdStringList != null
     144          && localSampleIdStringList.size() != 0) {
    164145        // Sort combined list
    165146        Collections.sort(localSampleIdStringList);
    166147        String tmpLocalSampleId = new String("");
    167         for (String partStr : localSampleIdStringList)
    168         {
    169           if (partStr != null && !partStr.equals(""))
    170           {
    171             if (!tmpLocalSampleId.equals(""))
    172             {
     148        for (String partStr : localSampleIdStringList) {
     149          if (partStr != null && !partStr.equals("")) {
     150            if (!tmpLocalSampleId.equals("")) {
    173151              // Append comma to separate part strings
    174152              tmpLocalSampleId += ",";
     
    180158      }
    181159    }
    182     if (localSampleId != null && localSampleId.length() > 0)
    183     {
    184       log
    185         .info("Only importing peaklists for local sample ID:" + localSampleId);
    186     }
    187     else
    188     {
     160    if (localSampleId != null && localSampleId.length() > 0) {
     161      log.info("Only importing peaklists for local sample ID:"
     162          + localSampleId);
     163    } else {
    189164      log.warn("No local sample ID given");
    190165      err.add(new BaseException("No local sample ID given."));
     
    193168    boolean imported = false;
    194169    FileType ft = f.getFileType();
    195     if (ft == null)
    196     {
     170    if (ft == null) {
    197171      ft = PeakListFileValidationFactory.retrieveFileTypeByValidation(f);
    198172    }
    199173    // }
    200     if (ft != null && (fts.contains(ft)))
    201     {
     174    if (ft != null && (fts.contains(ft))) {
    202175      // Retrieve replicate ID
    203176      String replicate = fetchReplicateIdFromFileAnnotation(dc, f);
    204177      // Attempt to obtain fraction ID from file annotation "fraction".
    205178      String fractionIdFromAnnotation = fetchFractionIdFromFileAnnotation(
    206         dc, f);
     179          dc, f);
    207180      // Use regex pattern to parse fraction ID from filename
    208181      String fractionIdFromFilename = null;
    209       if (regexPattern != null && !regexPattern.equals(""))
    210       {
     182      if (regexPattern != null && !regexPattern.equals("")) {
    211183        // Use regex pattern to parse fraction ID from filename
    212184        fractionIdFromFilename = fpu.fetchRegexResult(f.getName(),
    213           regexPattern);
    214       }
    215       log
    216         .debug("fraction id from file annotation = \"" + fractionIdFromAnnotation + "\"");
    217       log
    218         .debug("fraction id from file name = \"" + fractionIdFromFilename + "\"");
     185            regexPattern);
     186      }
     187      log.debug("fraction id from file annotation = \""
     188          + fractionIdFromAnnotation + "\"");
     189      log.debug("fraction id from file name = \""
     190          + fractionIdFromFilename + "\"");
    219191      // Get fraction id, if any
    220192      String fractionId = new String("");
    221193      if (fractionIdEntryMode
    222         .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_ANNOTATION) || fractionIdEntryMode
    223         .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_ANNOTATION_OR_NAME))
    224       {
    225         if (fractionIdFromAnnotation != null)
    226         {
     194          .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_ANNOTATION)
     195          || fractionIdEntryMode
     196              .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_ANNOTATION_OR_NAME)) {
     197        if (fractionIdFromAnnotation != null) {
    227198          // First choice, fraction id from annotation for peaklist
    228199          // file
    229200          fractionId = fractionIdFromAnnotation;
    230         }
    231         else if (fractionIdEntryMode
    232           .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_ANNOTATION_OR_NAME))
    233         {
    234           if (fractionIdFromFilename != null)
    235           {
     201        } else if (fractionIdEntryMode
     202            .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_ANNOTATION_OR_NAME)) {
     203          if (fractionIdFromFilename != null) {
    236204            // Second choice, fraction id from name of peaklist file
    237205            fractionId = fractionIdFromFilename;
    238206          }
    239207        }
    240       }
    241       else if (fractionIdEntryMode
    242         .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_NAME) || fractionIdEntryMode
    243         .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_NAME_OR_ANNOTATION))
    244       {
    245         if (fractionIdFromFilename != null)
    246         {
     208      } else if (fractionIdEntryMode
     209          .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_NAME)
     210          || fractionIdEntryMode
     211              .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_NAME_OR_ANNOTATION)) {
     212        if (fractionIdFromFilename != null) {
    247213          // First choice, fraction id from name of peaklist file
    248214          fractionId = fractionIdFromFilename;
    249         }
    250         else if (fractionIdEntryMode
    251           .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_NAME_OR_ANNOTATION))
    252         {
    253           if (fractionIdFromAnnotation != null)
    254           {
     215        } else if (fractionIdEntryMode
     216            .equals(GET_FRACTION_ID_FROM_PEAKLIST_FILE_NAME_OR_ANNOTATION)) {
     217          if (fractionIdFromAnnotation != null) {
    255218            // Second choice, fraction id from annotation for
    256219            // peaklist file
     
    259222        }
    260223      }
    261       log
    262         .info("peak list, fraction id: \"" + fractionId + "\" localsample Id: \"" + localSampleId + "\"");
     224      log.info("peak list, fraction id: \"" + fractionId
     225          + "\" localsample Id: \"" + localSampleId + "\"");
    263226      // Check that length of fraction ID is not too long
    264       if (fractionId != null && fractionId.length() > Hit.MAX_FRACTION_ID_LENGTH)
    265       {
    266         log
    267           .debug("Fraction ID = \"" + fractionId + "\" is too long (" + fractionId
    268             .length() + " characters, " + Hit.MAX_FRACTION_ID_LENGTH + " allowed).");
    269         err
    270           .add(new BaseException(
    271             "Fraction ID = \"" + fractionId + "\" is too long (" + fractionId
    272               .length() + " characters, " + Hit.MAX_FRACTION_ID_LENGTH + " allowed)."));
     227      if (fractionId != null
     228          && fractionId.length() > Hit.MAX_FRACTION_ID_LENGTH) {
     229        log.debug("Fraction ID = \"" + fractionId + "\" is too long ("
     230            + fractionId.length() + " characters, "
     231            + Hit.MAX_FRACTION_ID_LENGTH + " allowed).");
     232        err.add(new BaseException("Fraction ID = \"" + fractionId
     233            + "\" is too long (" + fractionId.length()
     234            + " characters, " + Hit.MAX_FRACTION_ID_LENGTH
     235            + " allowed)."));
    273236        response.setError("Nothing imported", err);
    274237        return;
     
    278241      ItemQuery<Hit> hitQuery = Hit.getQuery(project);
    279242      hitQuery.restrict(Restrictions.eq(Hql.property("fractionId"),
    280         Expressions.parameter("fractionId")));
     243          Expressions.parameter("fractionId")));
    281244      hitQuery.setParameter("fractionId", fractionId, null);
    282245      hitQuery.restrict(Restrictions.eq(Hql.property("localSampleId"),
    283         Expressions.parameter("localSampleId")));
     246          Expressions.parameter("localSampleId")));
    284247      hitQuery.setParameter("localSampleId", localSampleId, null);
    285       hitQuery.restrict(Restrictions.eq(Hql.property("project"), Hql
    286         .entity(project)));
     248      hitQuery.restrict(Restrictions.eq(Hql.property("project"),
     249          Hql.entity(project)));
    287250      hitQuery.setCacheResult(false);
    288251      ItemResultIterator<Hit> iri = hitQuery.iterate(dc);
    289       if (iri.hasNext())
    290       {
     252      if (iri.hasNext()) {
    291253        Hit hit = iri.next();
    292         if (hit.getPeakListFile() != null)
    293         {
    294           if (hit.getPeakListFile() == f)
    295           {
     254        if (hit.getPeakListFile() != null) {
     255          if (hit.getPeakListFile() == f) {
    296256            peaklistFileAlreadyRegistered = true;
    297257          }
    298258        }
    299259      }
    300       if (!peaklistFileAlreadyRegistered)
    301       {
     260      if (!peaklistFileAlreadyRegistered) {
    302261        // No hits with current local sample id and fraction id
    303262        ItemFactory factory = new ItemFactory(dc);
     
    307266        newhit.setLocalSampleId(localSampleId);
    308267        newhit.setFractionId(fractionId);
    309         if (replicate !=null)
    310         {
     268        if (replicate != null) {
    311269          newhit.setReplicateId(replicate);
    312270        }
     
    315273        imported = true;
    316274        newhit.setPeakListFile(f);
    317       }
    318       else
    319       {
    320         if (err.size() == 0)
    321         {
    322           err
    323             .add(new BaseException(
    324               "File already registered for local sample ID \"" + localSampleId + "\" and fraction ID \"" + fractionId + "\"."));
     275      } else {
     276        if (err.size() == 0) {
     277          err.add(new BaseException(
     278              "File already registered for local sample ID \""
     279                  + localSampleId + "\" and fraction ID \""
     280                  + fractionId + "\"."));
    325281          response.setError("Nothing imported", err);
    326282        }
    327283      }
    328     }
    329     else
    330     {
    331       if (err.size() == 0)
    332       {
    333         err
    334           .add(new BaseException(
     284    } else {
     285      if (err.size() == 0) {
     286        err.add(new BaseException(
    335287            "Error registering file. Possible reason: Not a valid peak list file."));
    336288        response.setError("Nothing imported", err);
    337289      }
    338290    }
    339     if (imported)
    340     {
     291    if (imported) {
    341292      dc.commit();
    342293      response.setDone("File registered");
    343     }
    344     else
    345     {
    346       if (err.size() == 0)
    347       {
     294    } else {
     295      if (err.size() == 0) {
    348296        err.add(new BaseException("Error registering file"));
    349297        response.setError("Nothing imported", err);
     
    353301  }
    354302
    355 
    356303  /*
    357304   * Attempts to obtain replicate id from file annotation "replicate".
     305   *
    358306   * @param DbControl dc The DbControl to use.
     307   *
    359308   * @param File file The file to get the annotation for.
     309   *
    360310   * @return String The found fraction id, or null if none found.
    361311   */
    362   private String fetchReplicateIdFromFileAnnotation(DbControl dc, File file)
    363   {
     312  private String fetchReplicateIdFromFileAnnotation(DbControl dc, File file) {
    364313    String replicateId = null;
    365314    Annotator annotator = new Annotator(new ItemFactory(dc));
    366     AnnotationType at = annotator.getAnnotationType(file, "replicate", Type.STRING);
    367     Annotation annotation = annotator.getAnnotation(file, at);
    368     Iterator<?> it = annotation.getValues().iterator();
    369     while (it.hasNext())
    370     {
    371       replicateId = (String) it.next();
     315    AnnotationType at = annotator.getAnnotationType(file, "replicate",
     316        Type.STRING);
     317    Annotation annotation = annotator.getExistingAnnotation(file, at);
     318    if (annotation != null) {
     319      Iterator<?> it = annotation.getValues().iterator();
     320      while (it.hasNext()) {
     321        replicateId = (String) it.next();
     322      }
    372323    }
    373324    return replicateId;
    374325  }
    375 
    376326
    377327  /*
    378328   * Attempts to obtain fraction id from file annotation "fraction".
     329   *
    379330   * @param DbControl dc The DbControl to use.
     331   *
    380332   * @param File file The file to get the anntation for.
     333   *
    381334   * @return String The found fraction id, or null if none found.
    382335   */
    383   private String fetchFractionIdFromFileAnnotation(DbControl dc, File file)
    384   {
     336  private String fetchFractionIdFromFileAnnotation(DbControl dc, File file) {
    385337    String fractionId = null;
    386338    // Try to get fraction id from file annotation "fraction"
    387339    Annotator annotator = new Annotator(new ItemFactory(dc));
    388340    ItemQuery<Annotation> annotationQuery = annotator
    389       .getAnnotationSet(file).getAnnotations();
     341        .getAnnotationSet(file).getAnnotations();
    390342    ItemResultList<Annotation> annotationList = annotationQuery.list(dc);
    391     if (annotationList != null && annotationList.size() > 0)
    392     {
    393       for (Annotation annotation : annotationList)
    394       {
    395         if (annotation != null)
    396         {
     343    if (annotationList != null && annotationList.size() > 0) {
     344      for (Annotation annotation : annotationList) {
     345        if (annotation != null) {
    397346          // Get annotation type
    398347          AnnotationType annotationType = annotation
    399             .getAnnotationType();
    400           if (annotationType != null)
    401           {
     348              .getAnnotationType();
     349          if (annotationType != null) {
    402350            String annotationTypeName = annotationType.getName();
    403             if (annotationTypeName != null && annotationTypeName
    404               .equals("fraction"))
    405             {
     351            if (annotationTypeName != null
     352                && annotationTypeName.equals("fraction")) {
    406353              List<String> tmpFractionIdList = (List<String>) annotation
    407                 .getValues();
    408               if (tmpFractionIdList != null && tmpFractionIdList
    409                 .size() > 0)
    410               {
     354                  .getValues();
     355              if (tmpFractionIdList != null
     356                  && tmpFractionIdList.size() > 0) {
    411357                String tmpFractionId = (String) tmpFractionIdList
    412                   .get(0);
    413                 if (tmpFractionId != null && !tmpFractionId
    414                   .equals(""))
    415                 {
     358                    .get(0);
     359                if (tmpFractionId != null
     360                    && !tmpFractionId.equals("")) {
    416361                  fractionId = tmpFractionId;
    417362                }
     
    425370  }
    426371
    427 
    428   public About getAbout()
    429   {
     372  public About getAbout() {
    430373    About about = new AboutImpl("PeakListToHitNonGelPlugin",
    431       "Registers peaklists for non-gel data to the hits report", "1.0",
    432       "", "Olle Månsson", "olle@thep.lu.se", "www.proteios.org");
     374        "Registers peaklists for non-gel data to the hits report",
     375        "1.0", "", "Olle Månsson", "olle@thep.lu.se",
     376        "www.proteios.org");
    433377    return about;
    434378  }
    435379
    436 
    437   public MainType getMainType()
    438   {
     380  public MainType getMainType() {
    439381    return MainType.IMPORT;
    440382  }
Note: See TracChangeset for help on using the changeset viewer.