Changeset 5552


Ignore:
Timestamp:
08/13/14 16:55:59 (10 years ago)
Author:
olhsha@mpi.nl
Message:

Fixed issued arising after testing on localhost tomcat (completed)

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

Legend:

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

    r5550 r5552  
    8484    private GroupService groupService;
    8585
    86    
    8786    //
    8887    /**
    8988     * Default constructor, to use this as a (spring) bean. The public registry
    90      * by default. Use setRegistryStatus(), setRegistryOwner(), setGroupId() to make it another kind of
    91      * registry.
     89     * by default. Use setRegistryStatus(), setRegistryOwner(), setGroupId() to
     90     * make it another kind of registry.
    9291     *
    93      * 
     92     *
    9493     */
    9594    public ComponentRegistryDbImpl() throws TransformerException {
     
    9796    }
    9897
    99 
    10098    @Override
    10199    public Owner getRegistryOwner() {
    102100        return this.registryOwner;
    103101    }
    104    
     102
    105103    @Override
    106104    public void setRegistryOwner(Owner registryOwner) {
    107105        this.registryOwner = registryOwner;
    108106    }
    109    
    110    
     107
    111108    @Override
    112109    public RegistrySpace getRegistrySpace() {
     
    123120        return this.groupId;
    124121    }
    125    
     122
    126123    @Override
    127124    public void setGroupId(Number groupId) {
    128125        this.groupId = groupId;
    129126    }
    130    
    131     @Override
    132     public Number getBaseDescriptionOwnerId(String cmdId){
     127
     128    @Override
     129    public Number getBaseDescriptionOwnerId(String cmdId) {
    133130        BaseDescription bd = componentDao.getByCmdId(cmdId);
    134131        return bd.getDbUserId();
    135132    }
    136    
    137     @Override
    138     public  List<Number> getItemGroups(String cmdId){
     133
     134    @Override
     135    public List<Number> getItemGroups(String cmdId) {
    139136        List<Group> groups = groupService.getGroupsTheItemIsAMemberOf(cmdId);
    140137        List<Number> result = new ArrayList<Number>();
    141138        if (groups != null) {
    142             for (Group group: groups) {
     139            for (Group group : groups) {
    143140                result.add(group.getId());
    144141            }
     
    146143        return result;
    147144    }
    148    
     145
    149146    /**
    150147     * @return The user id, or null if there is no owner or it is not a user.
     
    157154        }
    158155    }
    159    
     156
    160157    @Override
    161158    public List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException, UserUnauthorizedException {
     
    180177
    181178    @Override
    182     public Boolean isItemPublic(String id) throws ItemNotFoundException{
     179    public Boolean isItemPublic(String id) throws ItemNotFoundException {
    183180        BaseDescription desc = componentDao.getByCmdId(id);
    184181        if (desc == null) {
    185182            String idS = (id == null) ? "null" : id;
    186             throw new ItemNotFoundException("The component with the id " + idS+ "is not found in the database.");
     183            throw new ItemNotFoundException("The component with the id " + idS + "is not found in the database.");
    187184        }
    188185        return desc.isPublic();
     
    284281                    return comment;
    285282                } else {
    286                     throw new ComponentRegistryException("Comment " + commentId  + "for the profile "+profileId);
     283                    throw new ItemNotFoundException("Comment " + commentId + " for the profile " + profileId + " is not found.");
    287284                }
    288285            } catch (DataAccessException ex) {
     
    322319                    return comment;
    323320                } else {
    324                     throw new ComponentRegistryException("Comment " + commentId  + "for the profile "+componentId);
     321                    throw new ItemNotFoundException("Comment " + commentId + " for the component " + componentId + " is not found.");
    325322                }
    326323            } catch (DataAccessException ex) {
     
    437434    public int registerComment(Comment comment, String principalName) throws ComponentRegistryException, ItemNotFoundException, UserUnauthorizedException {
    438435        try {
    439             if (comment.getComponentId() != null)  {
    440                 if (this.canCurrentUserAccessDescription(comment.getComponentId()))
    441                 {
    442                 // Convert principal name to user record id
    443                 Number uid = convertUserIdInComment(comment, principalName);
    444                 // Set date to current date
    445                 comment.setCommentDate(new Date());
    446                 comment.setUserId(uid.longValue());
    447                 commentsDao.saveAndFlush(comment);
    448            
     436            if (comment.getComponentId() != null) {
     437                if (this.canCurrentUserAccessDescription(comment.getComponentId())) {
     438                    // Convert principal name to user record id
     439                    Number uid = convertUserIdInComment(comment, principalName);
     440                    // Set date to current date
     441                    comment.setCommentDate(new Date());
     442                    comment.setUserId(uid.longValue());
     443                    commentsDao.saveAndFlush(comment);
     444
     445                } else {
     446                    throw new UserUnauthorizedException("The logged-in user cannot access the component/profile with id " + comment.getComponentId());
     447                }
    449448            } else {
    450                 throw new UserUnauthorizedException("The logged-in user cannot access the component/profile with id "+comment.getComponentId());
    451             }
    452             } else {
    453                 throw new ComponentRegistryException("The component/profile id for this comment is null.");           
     449                throw new ComponentRegistryException("The component/profile id for this comment is null.");
    454450            }
    455451            return 0;
     
    623619    public void deleteMDComponent(String componentId, boolean forceDelete)
    624620            throws UserUnauthorizedException, DeleteFailedException, ComponentRegistryException, ItemNotFoundException {
    625         BaseDescription desc = componentDao.getByCmdId(componentId);
     621        BaseDescription desc = getComponentDescriptionAccessControlled(componentId);
    626622        if (desc != null) {
    627623            try {
     
    639635        }
    640636    }
    641 
    642    
    643637
    644638    private void invalidateCache(BaseDescription description) {
     
    704698
    705699    private void checkAuthorisation(BaseDescription desc) throws UserUnauthorizedException, ItemNotFoundException {
    706         if (!this.canCurrentUserAccessDescription(desc.getId())) {           
     700        if (!this.canCurrentUserAccessDescription(desc.getId())) {
    707701            String principalName = (registryOwner != null) ? userDao.getPrincipalNameById(registryOwner.getId()).getPrincipalName() : "null";
    708702            throw new UserUnauthorizedException("Unauthorized operation user '" + principalName
     
    715709        String principalName = userDao.getPrincipalNameById(registryOwner.getId()).getPrincipalName();
    716710        if (!(this.isOwnerOfComment(desc, principalName) || configuration.isAdminUser(principalName))) {
    717             throw new UserUnauthorizedException("Unauthorized operation user '" + principalName
    718                     + "' is not the creator (nor a member of the group, nor an administrator) of the " + (desc.getId()) + "(" + desc + ").");
     711            throw new UserUnauthorizedException("Unauthorized operation: user '" + principalName
     712                    + "' is not the creator (nor the Administrator) of the comment " + (desc.getId()));
    719713        }
    720714    }
     
    741735                throw new DeleteFailedException(
    742736                        "The "
    743                         + (desc.isProfile() ? "Profile" : "Component")
    744                         + " is more then a month old and cannot be deleted anymore. It might have been used to create metadata, deleting it would invalidate that metadata.");
     737                        + (desc.isProfile() ? "profile" : "Ccomponent")
     738                        + " is more than a month old and cannot be deleted anymore. It might have been used to create metadata, deleting it would invalidate that metadata.");
    745739            }
    746740        }
     
    766760    }
    767761
    768     private boolean canCurrentUserAccessDescription(String cmdId) throws ItemNotFoundException{
     762    private boolean canCurrentUserAccessDescription(String cmdId) throws ItemNotFoundException {
    769763        if (cmdId == null) {
    770764            throw new ItemNotFoundException("Item with the null cmdIdentifier.");
    771765        }
    772        
     766
    773767        BaseDescription description = componentDao.getByCmdId(cmdId);
    774768        if (description == null) {
    775             throw new ItemNotFoundException("Item with the id "+cmdId+" is not found.");
    776         }
    777        
    778        
     769            throw new ItemNotFoundException("Item with the id " + cmdId + " is not found.");
     770        }
     771
     772
    779773        Number userId = getUserId();
    780774        if (userId == null) {
     
    785779            return false;
    786780        }
    787        
     781
    788782
    789783        if (configuration.isAdminUser(user.getPrincipalName())) {
     
    805799
    806800    @Override
    807     public void deleteComment(String commentId) throws IOException, 
     801    public void deleteComment(String commentId) throws IOException,
    808802            UserUnauthorizedException, DeleteFailedException, ItemNotFoundException {
    809803        try {
     
    826820        }
    827821    }
    828    
    829  
    830822
    831823    @Override
     
    857849        for (String id : componentIds) {
    858850            BaseDescription description = componentDao.getByCmdId(id);
    859             components.add(ComponentUtils.toComponent(description));
     851            if (description != null) {
     852                components.add(ComponentUtils.toComponent(description));
     853            }
    860854        }
    861855        return components;
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/persistence/impl/ComponentDaoImpl.java

    r5549 r5552  
    2525 * Base DAO which can be extended to serve {@link ComponentDescription}s and
    2626 * {@link ProfileDescription}s
    27  * 
     27 *
    2828 * @author Twan Goosen <twan.goosen@mpi.nl>
    2929 * @author George.Georgovassilis@mpi.nl
    3030 */
    31 
    3231@Repository
    3332public class ComponentDaoImpl implements ComponentDao {
     
    3534    @Autowired
    3635    private JpaComponentDao jpaComponentDao;
    37 
    3836    @Autowired
    3937    private CommentsDao commentsDao;
    40 
    4138    @Autowired
    4239    private UserDao userDao;
    43 
    4440    private final static Logger LOG = LoggerFactory
    45             .getLogger(ComponentDaoImpl.class);
     41            .getLogger(ComponentDaoImpl.class);
    4642
    4743    /**
     
    4945     * thus making sure that only those properties are updated and not other
    5046     * ones.
    51      * 
     47     *
    5248     * @param from
    5349     * @param to
    5450     */
    5551    protected void copyPersistentProperties(BaseDescription from, BaseDescription to) {
    56         to.setName(from.getName());
    57         to.setDescription(from.getDescription());
    58         to.setRegistrationDate(from.getRegistrationDate());
    59         to.setCreatorName(from.getCreatorName());
    60         to.setDomainName(from.getDomainName());
    61         to.setGroupName(from.getGroupName());
    62         to.setHref(from.getHref());
     52        to.setName(from.getName());
     53        to.setDescription(from.getDescription());
     54        to.setRegistrationDate(from.getRegistrationDate());
     55        to.setCreatorName(from.getCreatorName());
     56        to.setDomainName(from.getDomainName());
     57        to.setGroupName(from.getGroupName());
     58        to.setHref(from.getHref());
    6359    }
    6460
    6561    protected String getTableName() {
    66         return "persistentcomponents";
    67     };
     62        return "persistentcomponents";
     63    }
     64
     65    ;
    6866
    6967    private boolean compare(String s, Number n) {
    70         return ("" + s).equals("" + n);
     68        return ("" + s).equals("" + n);
    7169    }
    7270
    7371    private String toString(Number n) {
    74         if (n == null)
    75             return null;
    76         return n.toString();
     72        if (n == null) {
     73            return null;
     74        }
     75        return n.toString();
    7776    }
    7877
    7978    private BaseDescription castDown(BaseDescription c) {
    80         // JPA will save only BaseComponent instances, not derived classes
    81         if (c instanceof ComponentDescription
    82                 || c instanceof ProfileDescription) {
    83             BaseDescription copy = new BaseDescription();
    84             ComponentUtils.copyPropertiesFrom(c, copy);
    85             return copy;
    86         }
    87         return c;
     79        // JPA will save only BaseComponent instances, not derived classes
     80        if (c instanceof ComponentDescription
     81                || c instanceof ProfileDescription) {
     82            BaseDescription copy = new BaseDescription();
     83            ComponentUtils.copyPropertiesFrom(c, copy);
     84            return copy;
     85        }
     86        return c;
    8887    }
    8988
     
    9291     * property) with the number of comments for that component and load
    9392     * {@link BaseDescription#getContent()}
    94      * 
     93     *
    9594     * @param baseDescription
    9695     */
    9796    private void augment(BaseDescription baseDescription) {
    98         if (baseDescription == null)
    99             return;
    100         int count = (int) commentsDao.findCommentCountForComponent(baseDescription
    101                 .getId());
    102         baseDescription.setCommentsCount(count);
    103         baseDescription.setContent(jpaComponentDao.findContentByComponentId(baseDescription
    104                 .getId()));
    105     }
    106 
    107 /**
     97        if (baseDescription == null) {
     98            return;
     99        }
     100        int count = (int) commentsDao.findCommentCountForComponent(baseDescription
     101                .getId());
     102        baseDescription.setCommentsCount(count);
     103        baseDescription.setContent(jpaComponentDao.findContentByComponentId(baseDescription
     104                .getId()));
     105    }
     106
     107    /**
    108108     * Update {@link BaseDescription#getCommentsCount()
     109     *
    109110     * @param baseDescription
    110111     */
    111112    private List<BaseDescription> augment(List<BaseDescription> baseDescription) {
    112         if (baseDescription.isEmpty()) {
    113             return baseDescription;
    114         }
    115         Map<String, BaseDescription> map = new HashMap<String, BaseDescription>();
    116         List<String> idlist = new ArrayList<String>();
    117         for (BaseDescription c:baseDescription) {
    118             idlist.add(c.getId());
    119             map.put(c.getId(), c);
    120         }
    121         List<Object[]> tuples = commentsDao
    122                 .findCommentCountForComponents(idlist);
    123         for (Object[] tuple : tuples) {
    124             String id = tuple[0].toString();
    125             int count = Integer.parseInt(tuple[1].toString());
    126             map.get(id).setCommentsCount(count);
    127         }
    128         return baseDescription;
    129     }
    130 
    131     /**
    132      *
    133      * @param cmdId
    134      *            CMD id
     113        if (baseDescription.isEmpty()) {
     114            return baseDescription;
     115        }
     116        Map<String, BaseDescription> map = new HashMap<String, BaseDescription>();
     117        List<String> idlist = new ArrayList<String>();
     118        for (BaseDescription c : baseDescription) {
     119            if (c != null) {
     120                idlist.add(c.getId());
     121                map.put(c.getId(), c);
     122            }
     123        }
     124        List<Object[]> tuples = commentsDao
     125                .findCommentCountForComponents(idlist);
     126        for (Object[] tuple : tuples) {
     127            String id = tuple[0].toString();
     128            int count = Integer.parseInt(tuple[1].toString());
     129            map.get(id).setCommentsCount(count);
     130        }
     131        return baseDescription;
     132    }
     133
     134    /**
     135     *
     136     * @param cmdId CMD id
    135137     * @return Whether the specified item is in the public space
    136138     */
    137139    @Override
    138140    public boolean isPublic(String cmdId) {
    139         BaseDescription baseDescription = jpaComponentDao.findByComponentId(cmdId);
    140         return baseDescription != null && baseDescription.isPublic();
    141     }
    142 
    143     /**
    144      *
    145      * @param cmdId
    146      *            CMD id
    147      * @param userId
    148      *            User db id of workspace owner
     141        BaseDescription baseDescription = jpaComponentDao.findByComponentId(cmdId);
     142        return baseDescription != null && baseDescription.isPublic();
     143    }
     144
     145    /**
     146     *
     147     * @param cmdId CMD id
     148     * @param userId User db id of workspace owner
    149149     * @return Whether the specified item is in the specified user's workspace
    150150     */
    151151    @Override
    152152    public boolean isInUserSpace(String cmdId, Number userId) {
    153         BaseDescription baseDescription = jpaComponentDao.findByComponentId(cmdId);
    154         boolean b = baseDescription != null && !baseDescription.isPublic()
    155                 && compare(baseDescription.getUserId(), userId);
    156         return b;
    157     }
    158 
    159     /**
    160      *
    161      * @param cmdId
    162      *            CMD id
    163      * @param userId
    164      *            User db id of workspace owner, null for public registry
     153        BaseDescription baseDescription = jpaComponentDao.findByComponentId(cmdId);
     154        boolean b = baseDescription != null && !baseDescription.isPublic()
     155                && compare(baseDescription.getUserId(), userId);
     156        return b;
     157    }
     158
     159    /**
     160     *
     161     * @param cmdId CMD id
     162     * @param userId User db id of workspace owner, null for public registry
    165163     * @return Whether the specified item is in the specified workspace (user or
    166      *         public)
     164     * public)
    167165     */
    168166//    @Override
     
    174172//      }
    175173//    }
    176 
    177     /**
    178      *
    179      * @param cmdId
    180      *            Profile or component Id (not primary key)
     174    /**
     175     *
     176     * @param cmdId Profile or component Id (not primary key)
    181177     * @return String value of XML content for profile or component
    182178     */
    183179    @Override
    184180    public String getContent(boolean isDeleted, String cmdId) {
    185         return jpaComponentDao.findContentByComponentId(cmdId, isDeleted);
    186     }
    187 
    188     /**
    189      * @param description
    190      *            Description to insert
    191      * @param content
    192      *            Content to insert and refer to from description
     181        return jpaComponentDao.findContentByComponentId(cmdId, isDeleted);
     182    }
     183
     184    /**
     185     * @param description Description to insert
     186     * @param content Content to insert and refer to from description
    193187     * @return Id of newly inserted description
    194188     */
    195189    @Override
    196190    public Number insertDescription(BaseDescription description, String content,
    197             boolean isPublic, Number userId) {
    198 
    199         if (description.getId() != null
    200                 && jpaComponentDao.findByComponentId(description.getId()) != null) {
    201             throw new IllegalArgumentException("Component "
    202                     + description.getId() + " already in DB");
    203         }
    204         if (content == null)
    205             content = "";
    206         BaseDescription copy = castDown(description);
    207         copy.setContent(content);
    208         copy.setPublic(isPublic);
    209         copy.setUserId(toString(userId));
     191            boolean isPublic, Number userId) {
     192
     193        if (description.getId() != null
     194                && jpaComponentDao.findByComponentId(description.getId()) != null) {
     195            throw new IllegalArgumentException("Component "
     196                    + description.getId() + " already in DB");
     197        }
     198        if (content == null) {
     199            content = "";
     200        }
     201        BaseDescription copy = castDown(description);
     202        copy.setContent(content);
     203        copy.setPublic(isPublic);
     204        copy.setUserId(toString(userId));
    210205        copy.setCreatorName(description.getCreatorName());
    211         copy = jpaComponentDao.saveAndFlush(copy);
    212         jpaComponentDao.updateContent(copy.getId(), content);
    213         return copy.getDbId();
     206        copy = jpaComponentDao.saveAndFlush(copy);
     207        jpaComponentDao.updateContent(copy.getId(), content);
     208        return copy.getDbId();
    214209    }
    215210
    216211    /**
    217212     * Updates a description by database id
    218      *
    219      * @param id
    220      *            Id (key) of description record
    221      * @param description
    222      *            New values for description (leave null to not change)
    223      * @param content
    224      *            New content for description (leave null to not change)
     213     *
     214     * @param id Id (key) of description record
     215     * @param description New values for description (leave null to not change)
     216     * @param content New content for description (leave null to not change)
    225217     */
    226218    @Override
    227219    public void updateDescription(Number id, BaseDescription description,
    228             String content) {
    229         if (content == null)
    230             content = "";
    231         if (description != null) {
    232             // Update description
    233             BaseDescription copy = jpaComponentDao.findByComponentId(description
    234                     .getId());
    235             copyPersistentProperties(description, copy);
    236             jpaComponentDao.saveAndFlush(copy);
    237             jpaComponentDao.updateContent(copy.getId(), content);
    238         }
    239 
    240         if (content != null) {
    241             BaseDescription copy = jpaComponentDao.findOne(id.longValue());
    242             copy.setContent(content);
    243             jpaComponentDao.saveAndFlush(copy);
    244             jpaComponentDao.updateContent(copy.getId(), content);
    245         }
     220            String content) {
     221        if (content == null) {
     222            content = "";
     223        }
     224        if (description != null) {
     225            // Update description
     226            BaseDescription copy = jpaComponentDao.findByComponentId(description
     227                    .getId());
     228            copyPersistentProperties(description, copy);
     229            jpaComponentDao.saveAndFlush(copy);
     230            jpaComponentDao.updateContent(copy.getId(), content);
     231        }
     232
     233        if (content != null) {
     234            BaseDescription copy = jpaComponentDao.findOne(id.longValue());
     235            copy.setContent(content);
     236            jpaComponentDao.saveAndFlush(copy);
     237            jpaComponentDao.updateContent(copy.getId(), content);
     238        }
    246239    }
    247240
    248241    /**
    249242     * Retrieves description by it's primary key Id
    250      *
    251      * @param id
    252      *            Description key
     243     *
     244     * @param id Description key
    253245     * @return The description, if it exists; null otherwise
    254246     */
    255247    @Override
    256248    public BaseDescription getById(Number id) {
    257         BaseDescription baseDescription = jpaComponentDao.findOne(id.longValue());
    258         if (baseDescription != null && !baseDescription.isDeleted()) {
    259             augment(baseDescription);
    260             return baseDescription;
    261         }
    262         return null;
     249        BaseDescription baseDescription = jpaComponentDao.findOne(id.longValue());
     250        if (baseDescription != null && !baseDescription.isDeleted()) {
     251            augment(baseDescription);
     252            return baseDescription;
     253        }
     254        return null;
    263255    }
    264256
    265257    /**
    266258     * Get by ComponentId / ProfileId, whether in userspace or public
    267      *
    268      * @param id
    269      *            Full component id
     259     *
     260     * @param id Full component id
    270261     * @return The description, if it exists; null otherwise
    271262     */
    272263    @Override
    273264    public BaseDescription getByCmdId(String id) {
    274         BaseDescription baseDescription = jpaComponentDao.findByComponentId(id);
    275         if (baseDescription != null && !baseDescription.isDeleted()) {
    276             augment(baseDescription);
    277             return baseDescription;
    278         }
    279         return null;
    280     }
    281 
    282  
    283     /**
    284      *
    285      * @param cmdId
    286      *            CMD Id of description
     265        BaseDescription baseDescription = jpaComponentDao.findByComponentId(id);
     266        if (baseDescription != null && !baseDescription.isDeleted()) {
     267            augment(baseDescription);
     268            return baseDescription;
     269        }
     270        return null;
     271    }
     272
     273    /**
     274     *
     275     * @param cmdId CMD Id of description
    287276     * @return Database id for description record
    288277     */
    289278    @Override
    290279    public Number getDbId(String cmdId) {
    291         Number id = null;
    292         BaseDescription c = jpaComponentDao.findByComponentId(cmdId);
    293         if (c != null)
    294             id = c.getDbId();
    295         return id;
    296     }
    297 
    298     /**
    299      *
     280        Number id = null;
     281        BaseDescription c = jpaComponentDao.findByComponentId(cmdId);
     282        if (c != null) {
     283            id = c.getDbId();
     284        }
     285        return id;
     286    }
     287
     288    /**
     289     *
    300290     * @return All descriptions in the public space. Won't include xml content
    301      *         and comments count.
     291     * and comments count.
    302292     */
    303293    @Override
    304294    public List<BaseDescription> getPublicDescriptions() {
    305         return augment(jpaComponentDao.findPublicItems());
     295        return augment(jpaComponentDao.findPublicItems());
    306296    }
    307297
    308298    /**
    309299     * @return List of deleted descriptions in user space or in public when
    310      *         userId=null. Won't include xml content and comments count.
     300     * userId=null. Won't include xml content and comments count.
    311301     * @param userId
    312302     */
    313303    @Override
    314304    public List<BaseDescription> getDeletedDescriptions(Number userId) {
    315         List<BaseDescription> list = null;
    316         if (userId != null) {
    317             list = jpaComponentDao.findDeletedItemsForUser(userId.longValue());
    318         } else {
    319             list = jpaComponentDao.findPublicDeletedItems();
    320         }
    321         list = augment(list);
    322         return list;
    323     }
    324 
    325     /**
    326      * 
     305        List<BaseDescription> list = null;
     306        if (userId != null) {
     307            list = jpaComponentDao.findDeletedItemsForUser(userId.longValue());
     308        } else {
     309            list = jpaComponentDao.findPublicDeletedItems();
     310        }
     311        list = augment(list);
     312        return list;
     313    }
     314
     315    /**
     316     *
    327317     * @return All the user's descriptions not in the public space .Won't
    328      *         include xml content and comments count.
     318     * include xml content and comments count.
    329319     */
    330320    @Override
    331321    public List<BaseDescription> getPrivateBaseDescriptions(Number userId, String prefix) {
    332         return augment(jpaComponentDao.findItemsForUserThatAreNotInGroups(userId.longValue(),prefix + "%"));
    333         //return augment(jpaComponentDao.findNotPublishedUserItems(userId.longValue(),prefix + "%"));
    334     }
    335 
    336    
     322        return augment(jpaComponentDao.findItemsForUserThatAreNotInGroups(userId.longValue(), prefix + "%"));
     323        //return augment(jpaComponentDao.findNotPublishedUserItems(userId.longValue(),prefix + "%"));
     324    }
     325
    337326    @Override
    338327    public void setDeleted(BaseDescription desc, boolean isDeleted) {
    339         BaseDescription copy = jpaComponentDao.findByComponentId(desc.getId());
    340         copy.setDeleted(isDeleted);
    341         jpaComponentDao.saveAndFlush(copy);
     328        BaseDescription copy = jpaComponentDao.findByComponentId(desc.getId());
     329        copy.setDeleted(isDeleted);
     330        jpaComponentDao.saveAndFlush(copy);
    342331    }
    343332
    344333    @Override
    345334    public void setPublished(Number id, boolean published) {
    346         BaseDescription copy = jpaComponentDao.findOne(id.longValue());
    347         copy.setPublic(published);
    348         jpaComponentDao.saveAndFlush(copy);
    349     }
    350 
    351     /**
    352      *
    353      * @param id
    354      *            Id of description record
     335        BaseDescription copy = jpaComponentDao.findOne(id.longValue());
     336        copy.setPublic(published);
     337        jpaComponentDao.saveAndFlush(copy);
     338    }
     339
     340    /**
     341     *
     342     * @param id Id of description record
    355343     * @return Principal name of description's owner, if any. Otherwise, null.
    356344     */
    357345    @Override
    358346    public String getOwnerPrincipalName(Number id) {
    359         BaseDescription baseDescription = getById(id);
    360         if (baseDescription == null)
    361             return null;
    362 
    363         long userId = baseDescription.getDbUserId();
    364         RegistryUser user = userDao.findOne(userId);
    365         if (user == null)
    366             return null;
    367 
    368         return user.getPrincipalName();
     347        BaseDescription baseDescription = getById(id);
     348        if (baseDescription == null) {
     349            return null;
     350        }
     351
     352        long userId = baseDescription.getDbUserId();
     353        RegistryUser user = userDao.findOne(userId);
     354        if (user == null) {
     355            return null;
     356        }
     357
     358        return user.getPrincipalName();
    369359    }
    370360
    371361    @Override
    372362    public List<BaseDescription> getPublicBaseDescriptions(String prefix) {
    373         return augment(jpaComponentDao.findPublishedItems(prefix + "%"));
    374     }
    375 
    376    
     363        return augment(jpaComponentDao.findPublishedItems(prefix + "%"));
     364    }
     365
    377366    @Override
    378367    public List<String> getAllNonDeletedProfileIds() {
    379         return jpaComponentDao.findNonDeletedItemIds(ProfileDescription.PROFILE_PREFIX+"%");
     368        return jpaComponentDao.findNonDeletedItemIds(ProfileDescription.PROFILE_PREFIX + "%");
    380369    }
    381370
    382371    @Override
    383372    public List<String> getAllNonDeletedComponentIds() {
    384         return jpaComponentDao.findNonDeletedItemIds(ComponentDescription.COMPONENT_PREFIX+"%");
    385     }
    386    
     373        return jpaComponentDao.findNonDeletedItemIds(ComponentDescription.COMPONENT_PREFIX + "%");
     374    }
     375
    387376    @Override
    388377    public List<BaseDescription> getAllNonDeletedDescriptions() {
    389         return jpaComponentDao.findNonDeletedDescriptions();
    390     }
    391    
     378        return jpaComponentDao.findNonDeletedDescriptions();
     379    }
     380
    392381    // Olha was here
    393382    @Override
    394383    public List<String> getAllItemIdsInGroup(String prefix, Long groupId) {
    395384        // we are ineterested only in non-published components in the group
    396         return jpaComponentDao.findAllItemIdsInGroup(false, prefix + "%", groupId);
    397     }
    398    
    399    
     385        return jpaComponentDao.findAllItemIdsInGroup(false, prefix + "%", groupId);
     386    }
    400387}
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r5551 r5552  
    189189                    "Releasing {} registered components into the world ({} millisecs)",
    190190                    result.size(), (System.currentTimeMillis() - start));
     191           
    191192            return result;
    192193        } catch (AuthenticationFailException e) {
     
    214215
    215216        long start = System.currentTimeMillis();
    216        
     217
    217218        if (!checkRegistrySpaceString(registrySpace)) {
    218219            response.sendError(Status.NOT_FOUND.getStatusCode(), "illegal registry space");
     
    225226                    "Releasing {} registered components into the world ({} millisecs)",
    226227                    result.size(), (System.currentTimeMillis() - start));
     228             
    227229            return result;
    228230        } catch (AuthenticationFailException e) {
     
    418420    public List<Comment> getCommentsFromProfile(
    419421            @PathParam("profileId") String profileId)
    420             throws ComponentRegistryException, IOException {
     422            throws IOException {
    421423        long start = System.currentTimeMillis();
    422424        try {
     
    426428                    comments.size(), (System.currentTimeMillis() - start));
    427429            return comments;
     430        } catch (ComponentRegistryException e) {
     431            response.sendError(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
     432            return new ArrayList<Comment>();
    428433        } catch (ItemNotFoundException e) {
    429             response.sendError(Status.NOT_FOUND.getStatusCode());
     434            response.sendError(Status.NOT_FOUND.getStatusCode(), e.getMessage());
    430435            return new ArrayList<Comment>();
    431 
    432436        } catch (UserUnauthorizedException e) {
    433             response.sendError(Status.FORBIDDEN.getStatusCode());
     437            response.sendError(Status.FORBIDDEN.getStatusCode(), e.getMessage());
    434438            return new ArrayList<Comment>();
    435439        } catch (AuthenticationFailException e1) {
    436             response.sendError(Status.UNAUTHORIZED.getStatusCode());
     440            response.sendError(Status.UNAUTHORIZED.getStatusCode(), e1.getMessage());
    437441            return new ArrayList<Comment>();
    438442        }
     
    446450    public List<Comment> getCommentsFromComponent(
    447451            @PathParam("componentId") String componentId)
    448             throws ComponentRegistryException, IOException {
     452            throws IOException {
    449453        long start = System.currentTimeMillis();
    450454        try {
     
    454458                    comments.size(), (System.currentTimeMillis() - start));
    455459            return comments;
     460        } catch (ComponentRegistryException e) {
     461            response.sendError(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
     462            return new ArrayList<Comment>();
    456463        } catch (ItemNotFoundException e) {
    457             response.sendError(Status.NOT_FOUND.getStatusCode());
     464            response.sendError(Status.NOT_FOUND.getStatusCode(), e.getMessage());
    458465            return new ArrayList<Comment>();
    459466        } catch (UserUnauthorizedException e1) {
    460             response.sendError(Status.FORBIDDEN.getStatusCode());
     467            response.sendError(Status.FORBIDDEN.getStatusCode(), e1.getMessage());
    461468            return new ArrayList<Comment>();
    462469        } catch (AuthenticationFailException e1) {
    463             response.sendError(Status.UNAUTHORIZED.getStatusCode());
     470            response.sendError(Status.UNAUTHORIZED.getStatusCode(), e1.getMessage());
    464471            return new ArrayList<Comment>();
    465472        }
     
    474481            @PathParam("profileId") String profileId,
    475482            @PathParam("commentId") String commentId)
    476             throws ComponentRegistryException, IOException {
     483            throws IOException {
    477484
    478485        LOG.debug("Comments of profile with id {} are requested.", commentId);
     
    480487
    481488            return this.getBaseRegistry().getSpecifiedCommentInProfile(profileId, commentId);
     489        } catch (ComponentRegistryException e) {
     490            response.sendError(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
     491            return new Comment();
    482492        } catch (ItemNotFoundException e) {
    483             response.sendError(Status.NOT_FOUND.getStatusCode());
     493            response.sendError(Status.NOT_FOUND.getStatusCode(), e.getMessage());
    484494            return new Comment();
    485495        } catch (UserUnauthorizedException e1) {
    486             response.sendError(Status.FORBIDDEN.getStatusCode());
     496            response.sendError(Status.FORBIDDEN.getStatusCode(), e1.getMessage());
    487497            return new Comment();
    488498        } catch (AuthenticationFailException e1) {
    489             response.sendError(Status.UNAUTHORIZED.getStatusCode());
     499            response.sendError(Status.UNAUTHORIZED.getStatusCode(), e1.getMessage());
    490500            return new Comment();
    491501        }
     
    500510            @PathParam("componentId") String componentId,
    501511            @PathParam("commentId") String commentId)
    502             throws ComponentRegistryException, IOException {
     512            throws IOException {
    503513        LOG.debug("Comments of component with id {} are requested.", commentId);
    504514        try {
    505515
    506516            return this.getBaseRegistry().getSpecifiedCommentInComponent(componentId, commentId);
     517        } catch (ComponentRegistryException e) {
     518            response.sendError(Status.INTERNAL_SERVER_ERROR.getStatusCode(), e.getMessage());
     519            return new Comment();
    507520        } catch (ItemNotFoundException e) {
    508             response.sendError(Status.NOT_FOUND.getStatusCode());
     521            response.sendError(Status.NOT_FOUND.getStatusCode(), e.getMessage());
    509522            return new Comment();
    510523        } catch (UserUnauthorizedException e1) {
    511             response.sendError(Status.FORBIDDEN.getStatusCode());
     524            response.sendError(Status.FORBIDDEN.getStatusCode(), e1.getMessage());
    512525            return new Comment();
    513526        } catch (AuthenticationFailException e1) {
    514             response.sendError(Status.UNAUTHORIZED.getStatusCode());
     527            response.sendError(Status.UNAUTHORIZED.getStatusCode(), e1.getMessage());
    515528            return new Comment();
    516529        }
     
    540553    }
    541554
    542     // TODO: test via POSTMAN
     555   
    543556    @Override
    544557    @POST
     
    555568    }
    556569
    557     // TODO: test via POSTMAN
     570   
    558571    @Override
    559572    @POST
     
    848861                    .build();
    849862        } catch (ItemNotFoundException e) {
    850             LOG.warn("Component with id " + componentId + " is not found.",
    851                     e);
     863            LOG.warn("Component with id " + componentId + " is not found.");
    852864            return Response.serverError().status(Status.NOT_FOUND)
    853865                    .build();
     
    869881
    870882        LOG.info("Component with id: {} deleted.", componentId);
    871         return Response.ok().build();
     883        return Response.ok("Component with id" + componentId+" deleted.").build();
    872884    }
    873885
     
    17361748    @Produces({MediaType.TEXT_XML, MediaType.APPLICATION_XML,
    17371749        MediaType.APPLICATION_JSON})
    1738     public String transferItemOwnershipToGroup(@PathParam("itemId") String itemId,
     1750    public Response transferItemOwnershipToGroup(@PathParam("itemId") String itemId,
    17391751            @QueryParam("groupId") long groupId) throws IOException {
    17401752        Principal principal = security.getUserPrincipal();
    17411753        try {
    17421754            groupService.transferItemOwnershipFromUserToGroupId(principal.getName(), groupId, itemId);
    1743             return "No exceptions happen, the item shoul be transferred";
     1755            return Response.ok("Ownership transferred").build();
    17441756        } catch (UserUnauthorizedException e) {
    1745             response.sendError(Status.FORBIDDEN.getStatusCode(), e.toString());
    1746             return e.toString();
     1757            return Response.status(Status.FORBIDDEN).build();
    17471758        }
    17481759    }
     
    17571768        try {
    17581769            ComponentRegistry cr = this.getBaseRegistry();
    1759             try {
    1760                 BaseDescription description = cr.getComponentDescriptionAccessControlled(itemId);
     1770            BaseDescription description;
     1771            if (itemId.startsWith(ComponentDescription.COMPONENT_PREFIX)) {
     1772                description = cr.getComponentDescriptionAccessControlled(itemId);
    17611773                return description;
    1762             } catch (UserUnauthorizedException ex1) {
    1763                 try {
    1764                     BaseDescription description = cr.getProfileDescriptionAccessControlled(itemId);
    1765                     return description;
    1766                 } catch (UserUnauthorizedException ex2) {
    1767                     response.sendError(Status.FORBIDDEN.getStatusCode(), "User \'" + security.getUserPrincipal().getName() + "\' does not have access to the item with the given id or the item with the given id does not exist.");
    1768                     return new BaseDescription();
    1769                 }
    1770             }
     1774            };
     1775            if (itemId.startsWith(ProfileDescription.PROFILE_PREFIX)) {
     1776                description = cr.getProfileDescriptionAccessControlled(itemId);
     1777                return description;
     1778            };
     1779            response.sendError(Status.BAD_REQUEST.getStatusCode());
     1780            return new BaseDescription();
     1781
     1782
     1783        } catch (UserUnauthorizedException ex2) {
     1784            response.sendError(Status.FORBIDDEN.getStatusCode(), ex2.getMessage());
     1785            return new BaseDescription();
    17711786        } catch (ItemNotFoundException e) {
    1772             response.sendError(Status.NOT_FOUND.getStatusCode());
     1787            response.sendError(Status.NOT_FOUND.getStatusCode(), e.getMessage());
    17731788            return new BaseDescription();
    17741789        } catch (AuthenticationFailException e) {
  • ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/IComponentRegistryRestService.java

    r5549 r5552  
    146146        void setComponentRegistryFactory(ComponentRegistryFactory componentRegistryFactory);
    147147
    148         /**
    149          *
    150          * @param userspace if "true" then profiles and components from the user's workspace, otherwise -- public
    151          * @param limit the number of items to be displayed
    152          * @return rss for the components in the database to which we are currently connected
    153          * @throws ComponentRegistryException
    154          * @throws ParseException
    155          */
     148       
    156149        Rss getRssComponent(String groupId, String registrySpace, String limit)
    157150                        throws ComponentRegistryException, ParseException, IOException;
    158151
    159         /**
    160          *
    161          * @param userspace if "true" then profiles and components from the user's workspace, otherwise -- public
    162          * @param limit the number of items to be displayed
    163          * @return rss for the profiles in the database to which we are currently connected
    164          * @throws ComponentRegistryException
    165          * @throws ParseException
    166          */
     152       
    167153        Rss getRssProfile(String groupId, String registrySpace, String limit)
    168154                        throws ComponentRegistryException, ParseException, IOException;
    169155
    170         /**
    171          *
    172          * @param profileId the Id of a profile whose comments are to be rss-ed
    173          * @param userspace if "true" then profiles and components from the user's workspace, otherwise -- public
    174          * @param limit the number of items to be displayed
    175          * @return rss of the comments for a chosen profile
    176          * @throws ComponentRegistryException
    177          * @throws IOException
    178          * @throws JAXBException
    179          * @throws ParseException
    180          */
     156       
    181157        Rss getRssOfCommentsFromProfile(String profileId, String limit) throws ComponentRegistryException,
    182158                        JAXBException, ParseException, IOException;
    183159
    184         /**
    185          *
    186          * @param componentId the Id of a component whose comments are to be rss-ed
    187          * @param userspace if "true" then profiles and components from the user's workspace, otherwise -- public
    188          * @param limit the number of items to be displayed
    189          * @return rss of the comments for a chosen component
    190          * @throws ComponentRegistryException
    191          * @throws IOException
    192          * @throws JAXBException
    193          * @throws ParseException
    194          */
     160       
    195161        Rss getRssOfCommentsFromComponent(String componentId,
    196162                        String limit) throws ComponentRegistryException, IOException,
     
    221187         * @param groupId
    222188         */
    223         String transferItemOwnershipToGroup(String itemId, long groupId) throws IOException;
     189        Response transferItemOwnershipToGroup(String itemId, long groupId) throws IOException;
    224190       
    225191        /**
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java

    r5550 r5552  
    328328                "/registry/components/" + id2 + "/comments/" + component1Comment4.getId()).delete(
    329329                ClientResponse.class);
    330         assertEquals(500, response.getStatus());
     330        assertEquals(404, response.getStatus());
    331331        // Delete from correct component
    332332        response = getAuthenticatedResource(
     
    403403                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    404404                + "profile2/comments/9999").delete(ClientResponse.class);
    405         assertEquals(500, response.getStatus());
     405        assertEquals(404, response.getStatus());
    406406        // Delete from correct profile
    407407        response = getAuthenticatedResource(
     
    492492                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
    493493                + "component1").delete(ClientResponse.class);
    494         assertEquals(200, response.getStatus());
     494        assertEquals(404, response.getStatus());
    495495    }
    496496
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/SanboxTest.java

    r5550 r5552  
    115115                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
    116116                + "profile2/comments/9999").delete(ClientResponse.class);
    117         assertEquals(500, response.getStatus());
     117        assertEquals(404, response.getStatus());
    118118        // Delete from correct profile
    119119        response = getAuthenticatedResource(
Note: See TracChangeset for help on using the changeset viewer.