Ignore:
Timestamp:
11/28/11 15:46:02 (13 years ago)
Author:
jeafer
Message:

Jean-Charles branch initial commit,
Changes regarding comment on the ComponentRegistry service.
Get comment from RestService? to Database access (CommentsDao?) implemented.
TestComment? class in development

Location:
ComponentRegistry/branches/jeaferversion
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry

    • Property svn:ignore
      •  

        old new  
        77profiles.xml
        88nb-configuration.xml
         9nbactions-Run without tests.xml
  • ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/impl/database/ComponentRegistryDbImpl.java

    r1604 r1631  
    2929import clarin.cmdi.componentregistry.impl.ComponentRegistryImplBase;
    3030import clarin.cmdi.componentregistry.model.AbstractDescription;
     31import clarin.cmdi.componentregistry.model.CommentMapping.Comment;
    3132import clarin.cmdi.componentregistry.model.ComponentDescription;
    3233import clarin.cmdi.componentregistry.model.ProfileDescription;
     
    4950    @Autowired
    5051    private ComponentDescriptionDao componentDescriptionDao;
    51     @Autowired
     52    @Autowired   
    5253    private UserDao userDao;
    5354    @Autowired
     
    5758    @Qualifier("profilesCache")
    5859    private CMDComponentSpecCache profilesCache;
     60    @Autowired
     61    private CommentsDao commentsDao;
     62    //private CMDComponentSpecCache commentsCache;
    5963
    6064    /**
     
    7680     */
    7781    public ComponentRegistryDbImpl(Number userId) {
    78         this.userId = userId;
     82        this.userId = userId;
    7983    }
    8084
    8185    @Override
    8286    public List<ProfileDescription> getProfileDescriptions() throws ComponentRegistryException {
    83         try {
    84             if (isPublic()) {
    85                 return profileDescriptionDao.getPublicProfileDescriptions();
    86             } else {
    87                 return profileDescriptionDao.getUserspaceDescriptions(getUserId());
    88             }
    89         } catch (DataAccessException ex) {
    90             throw new ComponentRegistryException("Database access error while trying to get profile descriptions", ex);
    91         }
    92     }
    93    
     87        try {
     88            if (isPublic()) {
     89                return profileDescriptionDao.getPublicProfileDescriptions();
     90            } else {
     91                return profileDescriptionDao.getUserspaceDescriptions(getUserId());
     92            }
     93        } catch (DataAccessException ex) {
     94            throw new ComponentRegistryException("Database access error while trying to get profile descriptions", ex);
     95        }
     96    }
     97
    9498    @Override
    9599    public ProfileDescription getProfileDescription(String id) throws ComponentRegistryException {
    96         try {
    97             return profileDescriptionDao.getByCmdId(id, getUserId());
    98         } catch (DataAccessException ex) {
    99             throw new ComponentRegistryException("Database access error while trying to get profile description", ex);
    100         }
     100        try {
     101            return profileDescriptionDao.getByCmdId(id, getUserId());
     102        } catch (DataAccessException ex) {
     103            throw new ComponentRegistryException("Database access error while trying to get profile description", ex);
     104        }
    101105    }
    102106
    103107    @Override
    104108    public List<ComponentDescription> getComponentDescriptions() throws ComponentRegistryException {
    105         try {
    106             if (isPublic()) {
    107                 return componentDescriptionDao.getPublicComponentDescriptions();
    108             } else {
    109                 return componentDescriptionDao.getUserspaceDescriptions(getUserId());
    110             }
    111         } catch (DataAccessException ex) {
    112             throw new ComponentRegistryException("Database access error while trying to get component descriptions", ex);
    113         }
     109        try {
     110            if (isPublic()) {
     111                return componentDescriptionDao.getPublicComponentDescriptions();
     112            } else {
     113                return componentDescriptionDao.getUserspaceDescriptions(getUserId());
     114            }
     115        } catch (DataAccessException ex) {
     116            throw new ComponentRegistryException("Database access error while trying to get component descriptions", ex);
     117        }
    114118    }
    115119
    116120    @Override
    117121    public ComponentDescription getComponentDescription(String id) throws ComponentRegistryException {
    118         try {
    119             return componentDescriptionDao.getByCmdId(id, getUserId());
    120         } catch (DataAccessException ex) {
    121             throw new ComponentRegistryException("Database access error while trying to get component description", ex);
    122         }
    123     }
     122        try {
     123            return componentDescriptionDao.getByCmdId(id, getUserId());
     124        } catch (DataAccessException ex) {
     125            throw new ComponentRegistryException("Database access error while trying to get component description", ex);
     126        }
     127    }
     128
     129    @Override
     130    public List<Comment> getCommentsInProfile(String profileId) throws ComponentRegistryException {
     131        try {
     132            return commentsDao.getCommentsFromProfile(profileId);
     133        } catch (DataAccessException ex) {
     134            throw new ComponentRegistryException("Database access error while trying to get comment from profile", ex);
     135        }
     136    }
     137
     138    @Override
     139    public Comment getSpecifiedCommentInProfile(String commentId) throws ComponentRegistryException {
     140        try {
     141            return commentsDao.getSpecifiedCommentFromProfile(commentId);
     142        } catch (DataAccessException ex) {
     143            throw new ComponentRegistryException("Database access error while trying to get comment from component", ex);
     144        }
     145    }
     146
     147    @Override
     148    public List<Comment> getCommentsInComponent(String componentId) throws ComponentRegistryException {
     149        try {
     150            return commentsDao.getCommentsFromComponent(componentId);
     151        } catch (DataAccessException ex) {
     152            throw new ComponentRegistryException("Database access error while trying to get comment from component", ex);
     153        }
     154    }
     155
     156    @Override
     157    public Comment getSpecifiedCommentInComponent(String commentId) throws ComponentRegistryException {
     158        try {
     159            return commentsDao.getSpecifiedCommentFromComponent(commentId);
     160        } catch (DataAccessException ex) {
     161            throw new ComponentRegistryException("Database access error while trying to get comment from component", ex);
     162        }
     163    }
     164
     165//    @Override
     166//    public List<Comment> getComments() throws ComponentRegistryException {
     167//        throw new UnsupportedOperationException("Not supported yet.");
     168//    }
    124169
    125170    @Override
    126171    public CMDComponentSpec getMDProfile(String id) throws ComponentRegistryException {
    127         if (inWorkspace(profileDescriptionDao, id)) {
    128             CMDComponentSpec result = profilesCache.get(id);
    129             if (result == null && !profilesCache.containsKey(id)) {
    130                 result = getUncachedMDProfile(id);
    131                 profilesCache.put(id, result);
    132             }
    133             return result;
    134         } else {
    135             // May exist, but not in this workspace
    136             return null;
    137         }
     172        if (inWorkspace(profileDescriptionDao, id)) {
     173            CMDComponentSpec result = profilesCache.get(id);
     174            if (result == null && !profilesCache.containsKey(id)) {
     175                result = getUncachedMDProfile(id);
     176                profilesCache.put(id, result);
     177            }
     178            return result;
     179        } else {
     180            // May exist, but not in this workspace
     181            return null;
     182        }
    138183    }
    139184
    140185    public CMDComponentSpec getUncachedMDProfile(String id) throws ComponentRegistryException {
    141         try {
    142             return getUncachedMDComponent(id, profileDescriptionDao);
    143         } catch (DataAccessException ex) {
    144             throw new ComponentRegistryException("Database access error while trying to get profile", ex);
    145         }
     186        try {
     187            return getUncachedMDComponent(id, profileDescriptionDao);
     188        } catch (DataAccessException ex) {
     189            throw new ComponentRegistryException("Database access error while trying to get profile", ex);
     190        }
    146191    }
    147192
    148193    @Override
    149194    public CMDComponentSpec getMDComponent(String id) throws ComponentRegistryException {
    150         if (inWorkspace(componentDescriptionDao, id)) {
    151             CMDComponentSpec result = componentsCache.get(id);
    152             if (result == null && !componentsCache.containsKey(id)) {
    153                 result = getUncachedMDComponent(id);
    154                 componentsCache.put(id, result);
    155             }
    156             return result;
    157         } else {
    158             return null;
    159         }
     195        if (inWorkspace(componentDescriptionDao, id)) {
     196            CMDComponentSpec result = componentsCache.get(id);
     197            if (result == null && !componentsCache.containsKey(id)) {
     198                result = getUncachedMDComponent(id);
     199                componentsCache.put(id, result);
     200            }
     201            return result;
     202        } else {
     203            return null;
     204        }
    160205    }
    161206
    162207    public CMDComponentSpec getUncachedMDComponent(String id) throws ComponentRegistryException {
    163         try {
    164             return getUncachedMDComponent(id, componentDescriptionDao);
    165         } catch (DataAccessException ex) {
    166             throw new ComponentRegistryException("Database access error while trying to get component", ex);
    167         }
    168     }
     208        try {
     209            return getUncachedMDComponent(id, componentDescriptionDao);
     210        } catch (DataAccessException ex) {
     211            throw new ComponentRegistryException("Database access error while trying to get component", ex);
     212        }
     213    }
     214
     215////    @Override
     216////    public CMDComponentSpec getMDComment(String id) throws ComponentRegistryException {
     217//////        if (inWorkspace(commentsDao, id)) {
     218//////            CMDComponentSpec result = commentsCache.get(id);
     219//////            if (result == null && !commentsCache.containsKey(id)) {
     220//////                result = getUncachedMDComment(id);
     221//////                commentsCache.put(id, result);
     222//////            }
     223//////            return result;
     224//////        } else {
     225//////            // May exist, but not in this workspace
     226////            return null;
     227//////       }
     228////    }
     229//
     230////    public CMDComponentSpec getUncachedMDComment(String id) throws ComponentRegistryException {
     231////        try {
     232////            return getUncachedMDComment(id, commentsDao);
     233////        } catch (DataAccessException ex) {
     234////            throw new ComponentRegistryException("Database access error while trying to get component", ex);
     235//       }
     236//  }
    169237
    170238    @Override
    171239    public int register(AbstractDescription description, CMDComponentSpec spec) {
    172         enrichSpecHeader(spec, description);
    173         try {
    174             String xml = componentSpecToString(spec);
    175             // Convert principal name to user record id
    176             Number uid = convertUserIdInDescription(description);
    177             getDaoForDescription(description).insertDescription(description, xml, isPublic(), uid);
    178             invalidateCache(description);
    179             return 0;
    180         } catch (DataAccessException ex) {
    181             LOG.error("Database error while registering component", ex);
    182             return -1;
    183         } catch (JAXBException ex) {
    184             LOG.error("Error while registering component", ex);
    185             return -2;
    186         } catch (UnsupportedEncodingException ex) {
    187             LOG.error("Error while registering component", ex);
    188             return -3;
    189         }
     240        enrichSpecHeader(spec, description);
     241        try {
     242            String xml = componentSpecToString(spec);
     243            // Convert principal name to user record id
     244            Number uid = convertUserIdInDescription(description);
     245            getDaoForDescription(description).insertDescription(description, xml, isPublic(), uid);
     246            invalidateCache(description);
     247            return 0;
     248        } catch (DataAccessException ex) {
     249            LOG.error("Database error while registering component", ex);
     250            return -1;
     251        } catch (JAXBException ex) {
     252            LOG.error("Error while registering component", ex);
     253            return -2;
     254        } catch (UnsupportedEncodingException ex) {
     255            LOG.error("Error while registering component", ex);
     256            return -3;
     257        }
    190258    }
    191259
     
    201269     */
    202270    private Number convertUserIdInDescription(AbstractDescription description) throws DataAccessException {
    203         Number uid = null;
    204         if (description.getUserId() != null) {
    205             User user = userDao.getByPrincipalName(description.getUserId());
    206             if (user != null) {
    207                 uid = user.getId();
    208             }
    209         } else {
    210             uid = userId;
    211         }
    212         if (uid != null) {
    213             description.setUserId(uid.toString());
    214         }
    215         return uid;
     271        Number uid = null;
     272        if (description.getUserId() != null) {
     273            User user = userDao.getByPrincipalName(description.getUserId());
     274            if (user != null) {
     275                uid = user.getId();
     276            }
     277        } else {
     278            uid = userId;
     279        }
     280        if (uid != null) {
     281            description.setUserId(uid.toString());
     282        }
     283        return uid;
    216284    }
    217285
    218286    @Override
    219287    public int update(AbstractDescription description, CMDComponentSpec spec, Principal principal, boolean forceUpdate) {
    220         try {
    221             checkAuthorisation(description, principal);
    222             checkAge(description, principal);
    223             // For public components, check if used in other components or profiles (unless forced)
    224             if (!forceUpdate && this.isPublic() && !description.isProfile()) {
    225                 checkStillUsed(description.getId());
    226             }
    227             AbstractDescriptionDao<?> dao = getDaoForDescription(description);
    228             dao.updateDescription(getIdForDescription(description), description, componentSpecToString(spec));
    229             invalidateCache(description);
    230             return 0;
    231         } catch (JAXBException ex) {
    232             LOG.error("Error while updating component", ex);
    233             return -1;
    234         } catch (UnsupportedEncodingException ex) {
    235             LOG.error("Error while updating component", ex);
    236             return -1;
    237         } catch (IllegalArgumentException ex) {
    238             LOG.error("Error while updating component", ex);
    239             return -1;
    240         } catch (UserUnauthorizedException e) {
    241             LOG.error("Error while updating component", e);
    242             return -1;
    243         } catch (DeleteFailedException e) {
    244             LOG.error("Error while updating component", e);
    245             return -1;
    246         } catch (ComponentRegistryException e) {
    247             LOG.error("Error while updating component", e);
    248             return -1;
    249         }
     288        try {
     289            checkAuthorisation(description, principal);
     290            checkAge(description, principal);
     291            // For public components, check if used in other components or profiles (unless forced)
     292            if (!forceUpdate && this.isPublic() && !description.isProfile()) {
     293                checkStillUsed(description.getId());
     294            }
     295            AbstractDescriptionDao<?> dao = getDaoForDescription(description);
     296            dao.updateDescription(getIdForDescription(description), description, componentSpecToString(spec));
     297            invalidateCache(description);
     298            return 0;
     299        } catch (JAXBException ex) {
     300            LOG.error("Error while updating component", ex);
     301            return -1;
     302        } catch (UnsupportedEncodingException ex) {
     303            LOG.error("Error while updating component", ex);
     304            return -1;
     305        } catch (IllegalArgumentException ex) {
     306            LOG.error("Error while updating component", ex);
     307            return -1;
     308        } catch (UserUnauthorizedException e) {
     309            LOG.error("Error while updating component", e);
     310            return -1;
     311        } catch (DeleteFailedException e) {
     312            LOG.error("Error while updating component", e);
     313            return -1;
     314        } catch (ComponentRegistryException e) {
     315            LOG.error("Error while updating component", e);
     316            return -1;
     317        }
    250318    }
    251319
    252320    @Override
    253321    public int publish(AbstractDescription desc, CMDComponentSpec spec, Principal principal) {
    254         int result = 0;
    255         AbstractDescriptionDao<?> dao = getDaoForDescription(desc);
    256         if (!isPublic()) { // if already in public workspace there is nothing todo
    257             desc.setHref(AbstractDescription.createPublicHref(desc.getHref()));
    258             Number id = getIdForDescription(desc);
    259             try {
    260                 // Update description & content
    261                 dao.updateDescription(id, desc, componentSpecToString(spec));
    262                 // Set to public
    263                 dao.setPublished(id, true);
    264             } catch (DataAccessException ex) {
    265                 LOG.error("Database error while updating component", ex);
    266                 return -1;
    267             } catch (JAXBException ex) {
    268                 LOG.error("Error while updating component", ex);
    269                 return -2;
    270             } catch (UnsupportedEncodingException ex) {
    271                 LOG.error("Error while updating component", ex);
    272                 return -3;
    273             }
    274         }
    275         return result;
     322        int result = 0;
     323        AbstractDescriptionDao<?> dao = getDaoForDescription(desc);
     324        if (!isPublic()) { // if already in public workspace there is nothing todo
     325            desc.setHref(AbstractDescription.createPublicHref(desc.getHref()));
     326            Number id = getIdForDescription(desc);
     327            try {
     328                // Update description & content
     329                dao.updateDescription(id, desc, componentSpecToString(spec));
     330                // Set to public
     331                dao.setPublished(id, true);
     332            } catch (DataAccessException ex) {
     333                LOG.error("Database error while updating component", ex);
     334                return -1;
     335            } catch (JAXBException ex) {
     336                LOG.error("Error while updating component", ex);
     337                return -2;
     338            } catch (UnsupportedEncodingException ex) {
     339                LOG.error("Error while updating component", ex);
     340                return -3;
     341            }
     342        }
     343        return result;
    276344    }
    277345
    278346    @Override
    279347    public void getMDProfileAsXml(String profileId, OutputStream output) throws ComponentRegistryException {
    280         CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandProfile(profileId, this);
    281         writeXml(expandedSpec, output);
     348        CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandProfile(profileId, this);
     349        writeXml(expandedSpec, output);
    282350    }
    283351
    284352    @Override
    285353    public void getMDProfileAsXsd(String profileId, OutputStream outputStream) throws ComponentRegistryException {
    286         CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandProfile(profileId, this);
    287         writeXsd(expandedSpec, outputStream);
     354        CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandProfile(profileId, this);
     355        writeXsd(expandedSpec, outputStream);
    288356    }
    289357
    290358    @Override
    291359    public void getMDComponentAsXml(String componentId, OutputStream output) throws ComponentRegistryException {
    292         CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandComponent(componentId, this);
    293         writeXml(expandedSpec, output);
     360        CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandComponent(componentId, this);
     361        writeXml(expandedSpec, output);
    294362    }
    295363
    296364    @Override
    297365    public void getMDComponentAsXsd(String componentId, OutputStream outputStream) throws ComponentRegistryException {
    298         CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandComponent(componentId, this);
    299         writeXsd(expandedSpec, outputStream);
     366        CMDComponentSpec expandedSpec = CMDComponentSpecExpanderDbImpl.expandComponent(componentId, this);
     367        writeXsd(expandedSpec, outputStream);
    300368    }
    301369
    302370    @Override
    303371    public void deleteMDProfile(String profileId, Principal principal) throws UserUnauthorizedException, DeleteFailedException,
    304             ComponentRegistryException {
    305         ProfileDescription desc = getProfileDescription(profileId);
    306         if (desc != null) {
    307             try {
    308                 checkAuthorisation(desc, principal);
    309                 checkAge(desc, principal);
    310                 profileDescriptionDao.setDeleted(desc, true);
    311                 invalidateCache(desc);
    312             } catch (DataAccessException ex) {
    313                 throw new DeleteFailedException("Database access error while trying to delete profile", ex);
    314             }
    315         }
    316     }
     372            ComponentRegistryException {
     373        ProfileDescription desc = getProfileDescription(profileId);
     374        if (desc != null) {
     375            try {
     376                checkAuthorisation(desc, principal);
     377                checkAge(desc, principal);
     378                profileDescriptionDao.setDeleted(desc, true);
     379                invalidateCache(desc);
     380            } catch (DataAccessException ex) {
     381                throw new DeleteFailedException("Database access error while trying to delete profile", ex);
     382            }
     383        }
     384    }
     385   
     386//    public void deleteMDComment(String commentId, Principal principal) throws UserUnauthorizedException, DeleteFailedException, ComponentRegistryException {
     387//        Comment desc = getCommentsInComponent(commentId);
     388//        if(desc != null) {
     389//            try {
     390//                checkAuthorisationComment(desc, principal);
     391//                checkCommentAge(desc, principal);
     392//                commentsDao.setDeleted(desc, true);
     393//                invalidateCommentCache(desc);
     394//            } catch (DataAccessException ex) {
     395//                throw new DeleteFailedException("Database access error while trying to delete profile", ex);
     396//            }
     397//        }
     398//    }
    317399
    318400    @Override
    319401    public void deleteMDComponent(String componentId, Principal principal, boolean forceDelete) throws UserUnauthorizedException,
    320             DeleteFailedException, ComponentRegistryException {
    321         ComponentDescription desc = componentDescriptionDao.getByCmdId(componentId);
    322         if (desc != null) {
    323             try {
    324                 checkAuthorisation(desc, principal);
    325                 checkAge(desc, principal);
    326 
    327                 if (!forceDelete) {
    328                     checkStillUsed(componentId);
    329                 }
    330                 componentDescriptionDao.setDeleted(desc, true);
    331                 invalidateCache(desc);
    332             } catch (DataAccessException ex) {
    333                 throw new DeleteFailedException("Database access error while trying to delete component", ex);
    334             }
    335         }
     402            DeleteFailedException, ComponentRegistryException {
     403        ComponentDescription desc = componentDescriptionDao.getByCmdId(componentId);
     404        if (desc != null) {
     405            try {
     406                checkAuthorisation(desc, principal);
     407                checkAge(desc, principal);
     408
     409                if (!forceDelete) {
     410                    checkStillUsed(componentId);
     411                }
     412                componentDescriptionDao.setDeleted(desc, true);
     413                invalidateCache(desc);
     414            } catch (DataAccessException ex) {
     415                throw new DeleteFailedException("Database access error while trying to delete component", ex);
     416            }
     417        }
    336418    }
    337419
    338420    @Override
    339421    public boolean isPublic() {
    340         return null == userId;
     422        return null == userId;
    341423    }
    342424
    343425    public void setPublic() {
    344         this.userId = null;
     426        this.userId = null;
    345427    }
    346428
     
    349431     */
    350432    public Number getUserId() {
    351         return userId;
     433        return userId;
    352434    }
    353435
     
    358440     */
    359441    public void setUserId(Number user) {
    360         this.userId = user;
     442        this.userId = user;
    361443    }
    362444
    363445    private void invalidateCache(AbstractDescription description) {
    364         if (description.isProfile()) {
    365             profilesCache.remove(description.getId());
    366         } else {
    367             componentsCache.remove(description.getId());
    368         }
    369     }
     446        if (description.isProfile()) {
     447            profilesCache.remove(description.getId());
     448        } else {
     449            componentsCache.remove(description.getId());
     450        }
     451    }
     452   
     453//        private void invalidateCommentCache(Comment comment) {
     454//            commentsCache.remove(comment.getId());
     455//    }
    370456
    371457    private AbstractDescriptionDao<?> getDaoForDescription(AbstractDescription description) {
    372         return description.isProfile() ? profileDescriptionDao : componentDescriptionDao;
    373     }
     458        return description.isProfile() ? profileDescriptionDao : componentDescriptionDao;
     459    }
     460   
     461//    private CommentsDao getDaoForCommentDescription(Comment comment) {
     462//        return commentsDao;
     463//    }
    374464
    375465    /**
     
    384474     */
    385475    private Number getIdForDescription(AbstractDescription description) throws IllegalArgumentException {
    386         Number dbId = null;
    387         AbstractDescriptionDao<?> dao = getDaoForDescription(description);
    388         try {
    389             dbId = dao.getDbId(description.getId());
    390         } catch (DataAccessException ex) {
    391             LOG.error("Error getting dbId for component with id " + description.getId(), ex);
    392         }
    393         if (dbId == null) {
    394             throw new IllegalArgumentException("Could not get database Id for description");
    395         } else {
    396             return dbId;
    397         }
    398     }
    399 
     476        Number dbId = null;
     477        AbstractDescriptionDao<?> dao = getDaoForDescription(description);
     478        try {
     479            dbId = dao.getDbId(description.getId());
     480        } catch (DataAccessException ex) {
     481            LOG.error("Error getting dbId for component with id " + description.getId(), ex);
     482        }
     483        if (dbId == null) {
     484            throw new IllegalArgumentException("Could not get database Id for description");
     485        } else {
     486            return dbId;
     487        }
     488    }
     489
     490   
     491//        private Number getIdForCommentDescription(Comment comment) throws IllegalArgumentException {
     492//        Number dbId = null;
     493//        CommentsDao dao = getDaoForCommentDescription(comment);
     494//        try {
     495//            dbId = dao.getDbId(comment.getId());
     496//        } catch (DataAccessException ex) {
     497//            LOG.error("Error getting dbId for comment with id " + comment.getId(), ex);
     498//        }
     499//        if (dbId == null) {
     500//            throw new IllegalArgumentException("Could not get database Id for description");
     501//        } else {
     502//            return dbId;
     503//        }
     504//    }
     505   
    400506    private String componentSpecToString(CMDComponentSpec spec) throws UnsupportedEncodingException, JAXBException {
    401         ByteArrayOutputStream os = new ByteArrayOutputStream();
    402         MDMarshaller.marshal(spec, os);
    403         String xml = os.toString("UTF-8");
    404         return xml;
     507        ByteArrayOutputStream os = new ByteArrayOutputStream();
     508        MDMarshaller.marshal(spec, os);
     509        String xml = os.toString("UTF-8");
     510        return xml;
    405511    }
    406512
    407513    private CMDComponentSpec getUncachedMDComponent(String id, AbstractDescriptionDao dao) {
    408         String xml = dao.getContent(false, id);
    409         if (xml != null) {
    410             try {
    411                 InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    412                 return MDMarshaller.unmarshal(CMDComponentSpec.class, is, null);
    413 
    414             } catch (JAXBException ex) {
    415                 LOG.error(null, ex);
    416             } catch(UnsupportedEncodingException ex){
    417                 LOG.error(null, ex);
    418             }
    419         }
    420         return null;
    421     }
     514        String xml = dao.getContent(false, id);
     515        if (xml != null) {
     516            try {
     517                InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
     518                return MDMarshaller.unmarshal(CMDComponentSpec.class, is, null);
     519
     520            } catch (JAXBException ex) {
     521                LOG.error(null, ex);
     522            } catch (UnsupportedEncodingException ex) {
     523                LOG.error(null, ex);
     524            }
     525        }
     526        return null;
     527    }
     528
     529//    private CMDComponentSpec getUncachedMDComment(String id, CommentsDao dao) {
     530//        String xml = dao.getContent(false, id);
     531//        if (xml != null) {
     532//            try {
     533//                InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
     534//                return MDMarshaller.unmarshal(CMDComponentSpec.class, is, null);
     535//
     536//            } catch (JAXBException ex) {
     537//                LOG.error(null, ex);
     538//            } catch (UnsupportedEncodingException ex) {
     539//                LOG.error(null, ex);
     540//            }
     541//        }
     542//        return null;
     543//    }
    422544
    423545    private void checkAuthorisation(AbstractDescription desc, Principal principal) throws UserUnauthorizedException {
    424         if (!isOwnerOfDescription(desc, principal.getName()) && !configuration.isAdminUser(principal)) {
    425             throw new UserUnauthorizedException("Unauthorized operation user '" + principal.getName()
    426                     + "' is not the creator (nor an administrator) of the " + (desc.isProfile() ? "profile" : "component") + "(" + desc
    427                     + ").");
    428         }
    429     }
     546        if (!isOwnerOfDescription(desc, principal.getName()) && !configuration.isAdminUser(principal)) {
     547            throw new UserUnauthorizedException("Unauthorized operation user '" + principal.getName()
     548                    + "' is not the creator (nor an administrator) of the " + (desc.isProfile() ? "profile" : "component") + "(" + desc
     549                    + ").");
     550        }
     551    }
     552   
     553//    private void checkAuthorisationComment(Comment desc, Principal principal)throws UserUnauthorizedException {
     554//        if (!isOwnerOfComment(desc, principal.getName()) && !configuration.isAdminUser(principal)) {
     555//            throw new UserUnauthorizedException("Unauthorized operation user '" + principal.getName()
     556//                    + "' is not the creator (nor an administrator) of the " + (desc.getId()) + "(" + desc
     557//                    + ").");
     558//        }
     559//    }
    430560
    431561    private boolean isOwnerOfDescription(AbstractDescription desc, String principalName) {
    432         String owner = getDaoForDescription(desc).getOwnerPrincipalName(getIdForDescription(desc));
    433         return owner != null // If owner is null, no one can be owner
    434                 && principalName.equals(owner);
    435     }
     562        String owner = getDaoForDescription(desc).getOwnerPrincipalName(getIdForDescription(desc));
     563        return owner != null // If owner is null, no one can be owner
     564                && principalName.equals(owner);
     565    }
     566   
     567//        private boolean isOwnerOfComment(Comment desc, String principalName) {
     568//        String owner = getDaoForCommentDescription(desc).getOwnerPrincipalName(getIdForCommentDescription(desc));
     569//        return owner != null // If owner is null, no one can be owner
     570//                && principalName.equals(owner);
     571//    }
    436572
    437573    private void checkAge(AbstractDescription desc, Principal principal) throws DeleteFailedException {
    438         if (isPublic() && !configuration.isAdminUser(principal)) {
    439             try {
    440                 Date regDate = AbstractDescription.getDate(desc.getRegistrationDate());
    441                 Calendar calendar = Calendar.getInstance();
    442                 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
    443                 if (regDate.before(calendar.getTime())) { // More then month old
    444                     throw new DeleteFailedException(
    445                             "The "
    446                                     + (desc.isProfile() ? "Profile" : "Component")
    447                                     + " 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.");
    448                 }
    449             } catch (ParseException e) {
    450                 LOG.error("Cannot parse date of " + desc + " Error:" + e);
    451             }
    452         }
    453     }
     574        if (isPublic() && !configuration.isAdminUser(principal)) {
     575            try {
     576                Date regDate = AbstractDescription.getDate(desc.getRegistrationDate());
     577                Calendar calendar = Calendar.getInstance();
     578                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
     579                if (regDate.before(calendar.getTime())) { // More then month old
     580                    throw new DeleteFailedException(
     581                            "The "
     582                            + (desc.isProfile() ? "Profile" : "Component")
     583                            + " 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.");
     584                }
     585            } catch (ParseException e) {
     586                LOG.error("Cannot parse date of " + desc + " Error:" + e);
     587            }
     588        }
     589    }
     590   
     591//        private void checkCommentAge(Comment desc, Principal principal) throws DeleteFailedException {
     592//        if (isPublic() && !configuration.isAdminUser(principal)) {
     593//            try {
     594//                Date regDate = Comment.getDate(desc.getCommentDate());
     595//                Calendar calendar = Calendar.getInstance();
     596//                calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
     597//                if (regDate.before(calendar.getTime())) { // More then month old
     598//                    throw new DeleteFailedException(
     599//                            "The "
     600//                            + (desc.getId())
     601//                            + " is more then a month old and cannot be deleted anymore.");
     602//                }
     603//            } catch (ParseException e) {
     604//                LOG.error("Cannot parse date of " + desc + " Error:" + e);
     605//            }
     606//        }
     607//    }
    454608
    455609    private boolean inWorkspace(AbstractDescriptionDao<?> dao, String cmdId) {
    456         if (isPublic()) {
    457             return dao.isPublic(cmdId);
    458         } else {
    459             return dao.isInUserSpace(cmdId, getUserId());
    460         }
    461     }
     610        if (isPublic()) {
     611            return dao.isPublic(cmdId);
     612        } else {
     613            return dao.isInUserSpace(cmdId, getUserId());
     614        }
     615    }
     616
     617//    private boolean inWorkspace(CommentsDao dao, String cmdId) {
     618//        if (isPublic()) {
     619//            return dao.isPublic(cmdId);
     620//        } else {
     621//            return dao.isInUserSpace(cmdId, getUserId());
     622//        }
     623//    }
    462624
    463625    @Override
    464626    public String getName() {
    465         if (isPublic()) {
    466             return ComponentRegistry.PUBLIC_NAME;
    467         } else {
    468             return "User " + getUserId() + " Registry";
    469         }
     627        if (isPublic()) {
     628            return ComponentRegistry.PUBLIC_NAME;
     629        } else {
     630            return "User " + getUserId() + " Registry";
     631        }
    470632    }
    471633
    472634    @Override
    473635    public List<ProfileDescription> getDeletedProfileDescriptions() {
    474         return profileDescriptionDao.getDeletedDescriptions(getUserId());
     636        return profileDescriptionDao.getDeletedDescriptions(getUserId());
    475637    }
    476638
    477639    @Override
    478640    public List<ComponentDescription> getDeletedComponentDescriptions() {
    479         return componentDescriptionDao.getDeletedDescriptions(getUserId());
     641        return componentDescriptionDao.getDeletedDescriptions(getUserId());
    480642    }
    481643}
Note: See TracChangeset for help on using the changeset viewer.