Changeset 1992


Ignore:
Timestamp:
06/18/12 14:03:46 (12 years ago)
Author:
twagoo
Message:

Created 'ComponentStatus?' enum and 'Owner' classes, and replaced userId in ComponentRegistry by Status and Owner properties.

Refs #142 and #143

Location:
ComponentRegistry/trunk/ComponentRegistry/src
Files:
6 added
5 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/ComponentRegistry.java

    r1892 r1992  
    11package clarin.cmdi.componentregistry;
    2 
    3 import java.io.IOException;
    4 import java.io.OutputStream;
    5 import java.security.Principal;
    6 import java.util.List;
    72
    83import clarin.cmdi.componentregistry.components.CMDComponentSpec;
     
    116import clarin.cmdi.componentregistry.model.ComponentDescription;
    127import clarin.cmdi.componentregistry.model.ProfileDescription;
     8import java.io.IOException;
     9import java.io.OutputStream;
     10import java.security.Principal;
     11import java.util.List;
    1312
    1413public interface ComponentRegistry {
     
    1817
    1918    /**
    20      * 
     19     *
    2120     * @return List of component descriptions ordered by name ascending
    2221     * @throws ComponentRegistryException
     
    2726
    2827    /**
    29      * 
     28     *
    3029     * @return List of profile descriptions ordered by name ascending
    3130     * @throws ComponentRegistryException
     
    3433
    3534    /**
    36      * 
     35     *
    3736     * @return List of profile descriptions ordered by name ascending, only the ones marked for showing in metadata editor
    3837     * @throws ComponentRegistryException
     
    4746
    4847    /**
    49      * 
     48     *
    5049     * @return -1 if profile could not be registered
    5150     */
     
    5352
    5453    /**
    55      * 
     54     *
    5655     * @param comment
    5756     * @return -1 if comment could not be registered
     
    6059
    6160    /**
    62      * 
     61     *
    6362     * @return -1 if component could not be updated
    6463     */
     
    6665
    6766    /**
    68      * 
     67     *
    6968     * @return -1 if component could not be published. Published means move from
    70      *         current (private) workspace to public workspace.
     69     * current (private) workspace to public workspace.
    7170     */
    7271    int publish(AbstractDescription desc, CMDComponentSpec spec, Principal principal);
     
    8180
    8281    /**
    83      * 
     82     *
    8483     * @param profileId
    8584     * @param principal
    8685     * @throws IOException
    8786     * @throws UserUnauthorizedException
    88      *             thrown when principal does not match creator of profile
     87     * thrown when principal does not match creator of profile
    8988     * @throws DeleteFailedException
    9089     */
     
    9291
    9392    /**
    94      * 
     93     *
    9594     * @param componentId
    9695     * @param principal
    9796     * @param forceDelete
    98      *            ignores the fact that the component is still in use by other
    99      *            components and just deletes.
     97     * ignores the fact that the component is still in use by other
     98     * components and just deletes.
    10099     * @throws IOException
    101100     * @throws UserUnauthorizedException
    102      *             thrown when principal does not match creator of component
     101     * thrown when principal does not match creator of component
    103102     * @throws DeleteFailedException
    104103     */
     
    107106
    108107    /**
    109      * 
     108     *
    110109     * @param componentId
    111110     * @return List of ComponentDescriptions of Components that use the given
    112      *         Component.
     111     * Component.
    113112     */
    114113    List<ComponentDescription> getUsageInComponents(String componentId) throws ComponentRegistryException;
    115114
    116115    /**
    117      * 
     116     *
    118117     * @param componentId
    119118     * @return List of ProfileDescriptions of Profiles that use the given
    120      *         Component.
     119     * Component.
    121120     */
    122121    List<ProfileDescription> getUsageInProfiles(String componentId) throws ComponentRegistryException;
     
    125124     * Return true if this registry is the public registry as opposed to a
    126125     * registry used for the user privately.
    127      **/
     126     * */
    128127    boolean isPublic();
    129128
     129    ComponentStatus getStatus();
     130
     131    Owner getOwner();
     132
    130133    /**
    131134     * @return {@link ComponentRegistry.PUBLIC_NAME} or name of the user's workspace
     
    134137
    135138    /**
    136      * 
     139     *
    137140     * @return List of profile descriptions ordered by name ascending
    138141     */
     
    140143
    141144    /**
    142      * 
     145     *
    143146     * @return List of component descriptions ordered by name ascending
    144147     */
     
    146149
    147150    /**
    148      * 
     151     *
    149152     * @param profileId
    150153     * @return List of comments fro a specific profile
    151      * @throws ComponentRegistryException 
     154     * @throws ComponentRegistryException
    152155     */
    153156    List<Comment> getCommentsInProfile(String profileId, Principal principal) throws ComponentRegistryException;
    154157
    155158    /**
    156      * 
     159     *
    157160     * @param profileId Id of profile to retrieve comment from
    158161     * @param commentId Id of comment to retrieve
    159162     * @return a specific comment from a profile
    160      * @throws ComponentRegistryException 
     163     * @throws ComponentRegistryException
    161164     */
    162165    Comment getSpecifiedCommentInProfile(String profileId, String commentId, Principal principal) throws ComponentRegistryException;
    163166
    164167    /**
    165      * 
     168     *
    166169     * @param componentId
    167170     * @return List of comments from a specific component
    168      * @throws ComponentRegistryException 
     171     * @throws ComponentRegistryException
    169172     */
    170173    List<Comment> getCommentsInComponent(String componentId, Principal principal) throws ComponentRegistryException;
    171174
    172175    /**
    173      * 
     176     *
    174177     * @param componentId Id of component to retrieve comment from
    175178     * @param commentId Id of comment to retrieve
    176179     * @return a specific comment from a component
    177      * @throws ComponentRegistryException 
     180     * @throws ComponentRegistryException
    178181     */
    179182    Comment getSpecifiedCommentInComponent(String componentId, String commentId, Principal principal) throws ComponentRegistryException;
    180183
    181184    /**
    182      * 
     185     *
    183186     * @param commentId Id of comment to delete
    184187     * @param principal User principal
     
    186189     * @throws ComponentRegistryException When comment cannot be successfully retrieved from registry
    187190     * @throws UserUnauthorizedException
    188      * @throws DeleteFailedException 
     191     * @throws DeleteFailedException
    189192     */
    190193    public void deleteComment(String commentId, Principal principal) throws IOException, ComponentRegistryException, UserUnauthorizedException,
     
    192195
    193196    /**
    194      * 
     197     *
    195198     * @return a component specification expander for this registry
    196199     */
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r1895 r1992  
    22
    33import clarin.cmdi.componentregistry.CMDComponentSpecExpander;
     4import clarin.cmdi.componentregistry.ComponentRegistry;
     5import clarin.cmdi.componentregistry.ComponentRegistryException;
     6import clarin.cmdi.componentregistry.ComponentStatus;
     7import clarin.cmdi.componentregistry.Configuration;
     8import clarin.cmdi.componentregistry.DeleteFailedException;
     9import clarin.cmdi.componentregistry.MDMarshaller;
     10import clarin.cmdi.componentregistry.Owner;
     11import clarin.cmdi.componentregistry.OwnerGroup;
     12import clarin.cmdi.componentregistry.OwnerUser;
     13import clarin.cmdi.componentregistry.UserUnauthorizedException;
     14import clarin.cmdi.componentregistry.components.CMDComponentSpec;
     15import clarin.cmdi.componentregistry.impl.ComponentRegistryImplBase;
     16import clarin.cmdi.componentregistry.model.AbstractDescription;
     17import clarin.cmdi.componentregistry.model.Comment;
     18import clarin.cmdi.componentregistry.model.ComponentDescription;
     19import clarin.cmdi.componentregistry.model.ProfileDescription;
     20import clarin.cmdi.componentregistry.model.RegistryUser;
    421import java.io.ByteArrayInputStream;
    522import java.io.ByteArrayOutputStream;
     
    1128import java.text.ParseException;
    1229import java.util.Calendar;
     30import java.util.Collection;
     31import java.util.Collections;
    1332import java.util.Date;
    1433import java.util.List;
    15 
    1634import javax.xml.bind.JAXBException;
    17 
    1835import org.slf4j.Logger;
    1936import org.slf4j.LoggerFactory;
     
    2138import org.springframework.beans.factory.annotation.Qualifier;
    2239import org.springframework.dao.DataAccessException;
    23 
    24 import clarin.cmdi.componentregistry.ComponentRegistry;
    25 import clarin.cmdi.componentregistry.ComponentRegistryException;
    26 import clarin.cmdi.componentregistry.Configuration;
    27 import clarin.cmdi.componentregistry.DeleteFailedException;
    28 import clarin.cmdi.componentregistry.MDMarshaller;
    29 import clarin.cmdi.componentregistry.UserUnauthorizedException;
    30 import clarin.cmdi.componentregistry.components.CMDComponentSpec;
    31 import clarin.cmdi.componentregistry.impl.ComponentRegistryImplBase;
    32 import clarin.cmdi.componentregistry.model.AbstractDescription;
    33 import clarin.cmdi.componentregistry.model.Comment;
    34 import clarin.cmdi.componentregistry.model.ComponentDescription;
    35 import clarin.cmdi.componentregistry.model.ProfileDescription;
    36 import clarin.cmdi.componentregistry.model.RegistryUser;
    37 import java.util.Collection;
    38 import java.util.Collections;
    3940
    4041/**
     
    4748
    4849    private final static Logger LOG = LoggerFactory.getLogger(ComponentRegistryDbImpl.class);
    49     private Number userId;
     50    private Owner owner;
     51    private ComponentStatus status;
    5052    @Autowired
    5153    private Configuration configuration;
     54    @Autowired
     55    @Qualifier("componentsCache")
     56    private CMDComponentSpecCache componentsCache;
     57    @Autowired
     58    @Qualifier("profilesCache")
     59    private CMDComponentSpecCache profilesCache;
     60    // DAO's
    5261    @Autowired
    5362    private ProfileDescriptionDao profileDescriptionDao;
     
    5766    private UserDao userDao;
    5867    @Autowired
    59     @Qualifier("componentsCache")
    60     private CMDComponentSpecCache componentsCache;
    61     @Autowired
    62     @Qualifier("profilesCache")
    63     private CMDComponentSpecCache profilesCache;
    64     @Autowired
    6568    private CommentsDao commentsDao;
    6669
    6770    /**
    68      * Default constructor, makes this a (spring) bean. No user is set, so
    69      * public registry by default. Use setUser() to make it a user registry.
     71     * Default constructor, to use this as a (spring) bean. The public registry by default.
     72     * Use setStatus() and setOwner() to make it another kind of registry.
    7073     *
    7174     * @see setUser
    7275     */
    7376    public ComponentRegistryDbImpl() {
     77        this.status = ComponentStatus.PUBLIC;
    7478    }
    7579
    7680    /**
    7781     * Creates a new ComponentRegistry (either public or not) for the provided
    78      * user
     82     * user. Only use for test and/or make sure to inject all dao's and other services
    7983     *
    8084     * @param userId
     
    8286     * public
    8387     */
    84     public ComponentRegistryDbImpl(Number userId) {
    85         this.userId = userId;
     88    public ComponentRegistryDbImpl(ComponentStatus status, Owner owner) {
     89        this.status = status;
     90        this.owner = owner;
    8691    }
    8792
     
    8994    public List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException {
    9095        try {
    91             if (isPublic()) {
    92                 return profileDescriptionDao.getPublicProfileDescriptions();
    93             } else {
    94                 return profileDescriptionDao.getUserspaceDescriptions(getUserId());
     96            switch (status) {
     97                // TODO: support other status types
     98                case DEVELOPMENT:
     99                    if (owner == null) {
     100                        throw new ComponentRegistryException("Development workspace without owner!");
     101                    }
     102                    // TODO: Support group space
     103                    return profileDescriptionDao.getUserspaceDescriptions(owner.getId());
     104                case PUBLIC:
     105                    return profileDescriptionDao.getPublicProfileDescriptions();
     106                default:
     107                    throw new ComponentRegistryException("Unsupported status type" + status);
    95108            }
    96109        } catch (DataAccessException ex) {
     
    331344            }
    332345        } else {
    333             uid = userId;
     346            uid = getUserId(); // this can be null as well
    334347        }
    335348        if (uid != null) {
     
    491504    }
    492505
    493     @Override
     506    /**
     507     *
     508     * @return whether this is the public registry
     509     * @deprecated use {@link #getStatus() } to check if this is the {@link ComponentStatus#PUBLIC public registry}
     510     */
     511    @Override
     512    @Deprecated
    494513    public boolean isPublic() {
    495         return null == userId;
    496     }
    497 
    498     public void setPublic() {
    499         this.userId = null;
    500     }
    501 
    502     /**
    503      * @return The user, or null if this is the public registry.
    504      */
    505     public Number getUserId() {
    506         return userId;
    507     }
    508 
    509     /**
    510      * @param User
    511      * for which this should be the registry. Pass null for the
    512      * public registry
    513      */
    514     public void setUserId(Number user) {
    515         this.userId = user;
     514        return status == ComponentStatus.PUBLIC;
     515    }
     516
     517    /**
     518     * @return The user id, or null if there is no owner or it is not a user.
     519     */
     520    private Number getUserId() {
     521        if (owner instanceof OwnerUser) {
     522            return owner.getId();
     523        } else {
     524            return null;
     525        }
     526    }
     527
     528    /**
     529     * @return The group id, or null if there is no owner or it is not a group.
     530     */
     531    private Number getGroupId() {
     532        if (owner instanceof OwnerGroup) {
     533            return owner.getId();
     534        } else {
     535            return null;
     536        }
     537    }
     538
     539    @Override
     540    public Owner getOwner() {
     541        return owner;
     542    }
     543
     544    /**
     545     * Sets status and owner of this registry
     546     *
     547     * @param status new status for registry
     548     * @param owner new owner for registry
     549     */
     550    public void setStatus(ComponentStatus status, Owner owner) {
     551        setStatus(status);
     552        this.owner = owner;
     553    }
     554
     555    public void setStatus(ComponentStatus status) {
     556        this.status = status;
     557    }
     558
     559    @Override
     560    public ComponentStatus getStatus() {
     561        return status;
    516562    }
    517563
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImpl.java

    r1863 r1992  
    1212import clarin.cmdi.componentregistry.ComponentRegistry;
    1313import clarin.cmdi.componentregistry.ComponentRegistryFactory;
     14import clarin.cmdi.componentregistry.ComponentStatus;
    1415import clarin.cmdi.componentregistry.Configuration;
     16import clarin.cmdi.componentregistry.OwnerUser;
    1517import clarin.cmdi.componentregistry.UserCredentials;
    1618import clarin.cmdi.componentregistry.model.RegistryUser;
     
    101103    private ComponentRegistry getNewComponentRegistryForUser(Number userId) {
    102104        ComponentRegistryDbImpl componentRegistry = componentRegistryBeanFactory.getNewComponentRegistry();
    103         componentRegistry.setUserId(userId);
     105        if (userId != null) {
     106            // Null means public registry
     107            // TODO: Make this more explicit
     108            componentRegistry.setStatus(ComponentStatus.DEVELOPMENT, new OwnerUser(userId));
     109        }
    104110        return componentRegistry;
    105111    }
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImplTest.java

    r1892 r1992  
    33import clarin.cmdi.componentregistry.ComponentRegistry;
    44import clarin.cmdi.componentregistry.ComponentRegistryException;
     5import clarin.cmdi.componentregistry.ComponentStatus;
    56import clarin.cmdi.componentregistry.DeleteFailedException;
     7import clarin.cmdi.componentregistry.OwnerGroup;
     8import clarin.cmdi.componentregistry.OwnerUser;
    69import clarin.cmdi.componentregistry.UserCredentials;
    710import clarin.cmdi.componentregistry.UserUnauthorizedException;
     
    374377    private ComponentRegistry getComponentRegistryForUser(Number userId) {
    375378        ComponentRegistryDbImpl componentRegistry = componentRegistryBeanFactory.getNewComponentRegistry();
    376         componentRegistry.setUserId(userId);
     379        if (userId != null) {
     380            componentRegistry.setStatus(ComponentStatus.DEVELOPMENT, new OwnerUser(userId));
     381        }
    377382        return componentRegistry;
    378383    }
     
    669674        assertEquals("0", comment.getUserId());
    670675        assertEquals(USER_CREDS.getDisplayName(), comment.getUserName());
    671        
     676
    672677        // Should not be allowed to delete, not authenticated
    673678        assertFalse(comment.isCanDelete());
     
    774779        assertEquals("Registry of J.Unit", register.getName());
    775780    }
     781
     782    @Test
     783    public void testGetStatus() {
     784        ComponentRegistry register = new ComponentRegistryDbImpl();
     785        assertEquals(ComponentStatus.PUBLIC, register.getStatus());
     786        register = new ComponentRegistryDbImpl(ComponentStatus.DEVELOPMENT, null);
     787        assertEquals(ComponentStatus.DEVELOPMENT, register.getStatus());
     788    }
     789
     790    @Test
     791    public void testGetOwner() {
     792        ComponentRegistry register = new ComponentRegistryDbImpl();
     793        assertNull(register.getOwner());
     794        register = new ComponentRegistryDbImpl(ComponentStatus.DEVELOPMENT, new OwnerUser(101));
     795        assertEquals(new OwnerUser(101), register.getOwner());
     796
     797        register = new ComponentRegistryDbImpl(ComponentStatus.DEVELOPMENT, new OwnerGroup(101));
     798        assertEquals(new OwnerGroup(101), register.getOwner());
     799    }
     800
     801    @Test
     802    public void testSetStatus() throws Exception {
     803        // Construct with an owner
     804        ComponentRegistryDbImpl register = new ComponentRegistryDbImpl(ComponentStatus.PUBLIC, new OwnerUser(101));
     805        register.setStatus(ComponentStatus.DEVELOPMENT);
     806        assertEquals(ComponentStatus.DEVELOPMENT, register.getStatus());
     807        // Owner should remain unchanged
     808        assertEquals(new OwnerUser(101), register.getOwner());
     809
     810        // Construct with no owner
     811        register = new ComponentRegistryDbImpl(ComponentStatus.PUBLIC, null);
     812        register.setStatus(ComponentStatus.DEVELOPMENT);
     813        assertEquals(ComponentStatus.DEVELOPMENT, register.getStatus());
     814        // Owner should remain unchanged
     815        assertNull(register.getOwner());
     816    }
     817
     818    @Test
     819    public void testSetStatusAndOwner() throws Exception {
     820        ComponentRegistryDbImpl register = new ComponentRegistryDbImpl(ComponentStatus.PUBLIC, new OwnerUser(101));
     821
     822        register.setStatus(ComponentStatus.DEVELOPMENT, new OwnerGroup(102));
     823        assertEquals(ComponentStatus.DEVELOPMENT, register.getStatus());
     824        assertEquals(new OwnerGroup(102), register.getOwner());
     825    }
    776826}
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryFactoryDbImplTest.java

    r1696 r1992  
    6666                getComponentRegistry(true, credentials);
    6767        assertNotNull(cr2);
    68         assertEquals(cr1.getUserId(), cr2.getUserId());
     68        assertEquals(cr1.getOwner(), cr2.getOwner());
    6969
    7070        // Get for another new user
     
    7474                getComponentRegistry(true, credentials2);
    7575        assertNotNull(cr3);
    76         assertNotSame(cr1.getUserId(), cr3.getUserId());
     76        assertNotSame(cr1.getOwner(), cr3.getOwner());
    7777    }
    7878
     
    8686                getComponentRegistry(true, userCredentials);
    8787
    88         String id = cr1.getUserId().toString();
     88        String id = cr1.getOwner().getId().toString();
    8989
    9090        // Get it as admin
     
    9393        assertNotNull(cr2);
    9494        // Should be this user's registry
    95         assertEquals(cr1.getUserId(), cr2.getUserId());
     95        assertEquals(cr1.getOwner(), cr2.getOwner());
    9696
    9797        // Try get it as non-admin
Note: See TracChangeset for help on using the changeset viewer.