Changeset 7299


Ignore:
Timestamp:
Feb 22, 2017, 12:59:59 PM (7 years ago)
Author:
Nicklas Nordborg
Message:

References #2056: Improve API for project-specific annotations

Changed API methods related to this to take a Project parameter instead of the numeric ID. This is to avoid permission issues that would make it possible to create annotations for projects that the logged in user is not a member of.

Location:
trunk/src
Files:
4 edited

Legend:

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

    r7298 r7299  
    172172  private final Map<String, UnitConverter> unitConvertes;
    173173 
     174  /**
     175    Create a new batcher instance. Using this constructor creates a batcher
     176    that, for project-specific annotation types, automatically handles either
     177    the default or project-specific value depending on the currently active
     178    project and wheter a value already exists or not.
     179     
     180    @param dc A DbControl to use for database access
     181    @param itemType The type of items to work with. This must be an annotatable item type.
     182  */
    174183  public AnnotationBatcher(DbControl dc, Item itemType)
    175184  {
    176     this(dc, itemType, null);
    177   }
    178  
     185    this(dc, itemType, (Integer)null);
     186  }
     187 
     188  /**
     189    Create a new batcher instance. Using this constructor creates a batcher
     190    that, for project-specific annotation types, always work with the values
     191    for the given project (or the default values if project=null). The currently
     192    active project is ignored.
     193     
     194    @param dc A DbControl to use for database access
     195    @param itemType The type of items to work with. This must be an annotatable item type.
     196    @param project Specify a project to only consider values for that project
     197      when working with a project-specific annotation, or null to only consider default
     198      values
     199    @since 3.11
     200  */
     201  public AnnotationBatcher(DbControl dc, Item itemType, Project project)
     202  {
     203    this(dc, itemType, project == null ? 0 : project.getId());
     204  }
     205
    179206  /**
    180207    Create a new batcher instance.
     
    188215  */
    189216  @SuppressWarnings({ "unchecked", "rawtypes" })
    190   public AnnotationBatcher(DbControl dc, Item itemType, Integer projectId)
     217  private AnnotationBatcher(DbControl dc, Item itemType, Integer projectId)
    191218  {
    192219    if (dc == null) throw new NullPointerException("dc");
  • trunk/src/core/net/sf/basedb/core/AnnotationSet.java

    r7296 r7299  
    602602 
    603603  /**
    604     Get a value for a project-specific annotation using the specified project id instead
     604    Get a value for a project-specific annotation using the specified project instead
    605605    of the currently active project. Note that this method behaves differently than
    606606    the {@link #getAnnotation(AnnotationType)} method.
    607607   
    608      * If the annotation type is not using project annotation, the project id is ignored
    609      * If projectId=0 the default annotation  value is returned (new or existing)
    610      * If projectId!=0 a project-specific annotation value is returned (new or existing,
     608     * If the annotation type is not using project annotations, the project is ignored
     609     * If project=null the default annotation  value is returned (new or existing)
     610     * If project!=null a project-specific annotation value is returned (new or existing,
    611611       the default value is never returned)
    612612       
     
    617617
    618618    @param annotationType The annotation type
    619     @param projectId The project id or 0 to get the default value
     619    @param project A project or null to get the default value
    620620    @since 3.11
    621621  */
    622   public Annotation getProjectAnnotation(AnnotationType annotationType, int projectId)
     622  public Annotation getProjectAnnotation(AnnotationType annotationType, Project project)
    623623  {
    624624    if (annotationType == null) throw new InvalidUseOfNullException("annotationType");
     
    626626   
    627627    int primary = Annotation.Source.PRIMARY.ordinal();
    628     if (!annotationType.getProjectAnnotations()) projectId = 0;
     628    int projectId = annotationType.getProjectAnnotations() && project != null ? project.getId() : 0;
    629629
    630630    // Search for an existing primary annotation
     
    734734    @param annotationType An <code>AnnotationType</code> object
    735735    @param source The source type of the annotation or null if it doesn't matter
    736     @param projectId The project id or 0 to get the default value
     736    @param project A project or null to check for the default value
    737737   
    738738    @return TRUE if the annotation set contains an annotation of
     
    740740    @since 3.11
    741741  */
    742   public boolean hasProjectAnnotation(AnnotationType annotationType, Annotation.Source source, int projectId)
     742  public boolean hasProjectAnnotation(AnnotationType annotationType, Annotation.Source source, Project project)
    743743  {
    744744    if (annotationType == null) return false;
    745745   
    746746    DbControl dc = getDbControl();
    747     if (!annotationType.getProjectAnnotations()) projectId = 0;
     747    int projectId = annotationType.getProjectAnnotations() && project != null ? project.getId() : 0;
    748748   
    749749    AnnotationTypeData atd = annotationType.getData();
     
    780780    throws PermissionDeniedException, InvalidDataException, BaseException
    781781  {
    782     removeProjectAnnotation(annotationType, getSessionControl().getActiveProjectId());
     782    int projectId = getSessionControl().getActiveProjectId();
     783    removeProjectAnnotation(annotationType, projectId == 0 ? null : Project.getById(getDbControl(), projectId));
    783784  }
    784785 
     
    791792   
    792793    @param annotationType The type of the annotation to delete
    793     @param projectId The project id or 0 to remove the default value
     794    @param project A project or null to remove the default value
    794795    @throws PermissionDeniedException If the logged in user
    795796      doesn't have write permission
     
    798799    @since 3.11
    799800  */
    800   public void removeProjectAnnotation(AnnotationType annotationType, int projectId)
     801  public void removeProjectAnnotation(AnnotationType annotationType, Project project)
    801802    throws PermissionDeniedException, InvalidDataException, BaseException
    802803  {
     
    804805    if (annotationType == null) throw new InvalidUseOfNullException("annotationType");
    805806    annotationType.checkPermission(Permission.USE);
    806     if (!annotationType.getProjectAnnotations()) projectId = 0;
     807    int projectId = annotationType.getProjectAnnotations() && project != null ? project.getId() : 0;
    807808   
    808809    DbControl dc = getDbControl();
     
    867868 
    868869    @param source A source or null to not filter on source
    869     @param projectId The project id or 0 to query default values
     870    @param project The project or null to query default values
    870871    @since 3.11
    871872  */
    872   public ItemQuery<Annotation> getProjectAnnotations(Annotation.Source source, int projectId)
     873  public ItemQuery<Annotation> getProjectAnnotations(Annotation.Source source, Project project)
    873874  {
    874875    ItemQuery<Annotation> query = Annotation.getQuery();
     
    881882      Restrictions.eq(
    882883        Hql.property("projectId"),
    883         Expressions.integer(projectId)
     884        Expressions.integer(project != null ? project.getId() : 0)
    884885    ));
    885886    if (source != null)
  • trunk/src/core/net/sf/basedb/core/snapshot/SnapshotManager.java

    r7297 r7299  
    3737import net.sf.basedb.core.DbControl;
    3838import net.sf.basedb.core.Permission;
     39import net.sf.basedb.core.Project;
    3940import net.sf.basedb.util.StaticCache;
    4041import net.sf.basedb.util.filter.Filter;
     
    121122 
    122123  /**
    123     When a project id is set, the snapshot manager will only
     124    When a project is set, the snapshot manager will only
    124125    search for project-specific annotations explicitely belonging
    125     to that project (or default values if projectId=0).
    126    
    127     Note that this parameter typically always need to be off (=null)
    128     when searching for annotations to an annotation type that
    129     doesn't have enabled project-specific annotations.
    130    
    131     @param projectId The project id or 0 to only search for default
    132       values or null to revert to the default behaviour
    133       (which load default values if a project-specific values
     126    to that project (or default values if project == null).
     127   
     128    Note that this option typically always need to be off
     129    ({@link #setNoProject()}) when searching for annotations to an
     130    annotation type that doesn't have enabled project-specific annotations.
     131   
     132    Calling this method with a null parameter IS NOT the same
     133    as calling {@link #setNoProject()}.
     134   
     135    @param project The project or null to only search for default
     136      values. Use {@link #setNoProject()} to revert to the default
     137      behaviour (which load default values if a project-specific values
    134138      doesn't exists)
    135139    @since 3.11
    136140  */
    137   public void setProjectId(Integer projectId)
    138   {
    139     this.projectId = projectId;
     141  public void setProject(Project project)
     142  {
     143    this.projectId = project == null ? 0 : project.getId();
     144  }
     145 
     146  /**
     147    Revert to the default bahavior for loading project-specific annotations.
     148   
     149    @since 3.11
     150  */
     151  public void setNoProject()
     152  {
     153    this.projectId = null;
    140154  }
    141155 
  • trunk/src/test/TestAnnotation.java

    r7296 r7299  
    281281      Annotatable annotatable = (Annotatable)itemType.getById(dc, itemId);
    282282      AnnotationType at = AnnotationType.getById(dc, annotationTypeId);
     283      Project project = projectId == 0 ? null : Project.getById(dc, projectId);
    283284
    284285      AnnotationSet as = annotatable.getAnnotationSet();
    285       Annotation a = as.getProjectAnnotation(at, projectId);
     286      Annotation a = as.getProjectAnnotation(at, project);
    286287     
    287288      if (value != null)
Note: See TracChangeset for help on using the changeset viewer.