source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/persistence/impl/ComponentDaoImpl.java @ 5549

Last change on this file since 5549 was 5549, checked in by olhsha@mpi.nl, 10 years ago

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File size: 11.8 KB
RevLine 
[3451]1package clarin.cmdi.componentregistry.persistence.impl;
[1297]2
[3863]3import java.util.ArrayList;
4import java.util.HashMap;
[1357]5import java.util.List;
[3863]6import java.util.Map;
[1357]7
8import org.slf4j.Logger;
9import org.slf4j.LoggerFactory;
[3770]10import org.springframework.beans.factory.annotation.Autowired;
[1326]11import org.springframework.dao.DataAccessException;
[3761]12import org.springframework.stereotype.Repository;
[1357]13
[3761]14import clarin.cmdi.componentregistry.impl.ComponentUtils;
[3897]15import clarin.cmdi.componentregistry.model.BaseDescription;
[3449]16import clarin.cmdi.componentregistry.model.ComponentDescription;
17import clarin.cmdi.componentregistry.model.ProfileDescription;
[3785]18import clarin.cmdi.componentregistry.model.RegistryUser;
[3761]19import clarin.cmdi.componentregistry.persistence.ComponentDao;
[3863]20import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao;
[3775]21import clarin.cmdi.componentregistry.persistence.jpa.JpaComponentDao;
[3785]22import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
[3449]23
[1297]24/**
[3761]25 * Base DAO which can be extended to serve {@link ComponentDescription}s and
26 * {@link ProfileDescription}s
27 *
[1297]28 * @author Twan Goosen <twan.goosen@mpi.nl>
[3449]29 * @author George.Georgovassilis@mpi.nl
[1297]30 */
31
[3761]32@Repository
[3863]33public class ComponentDaoImpl implements ComponentDao {
[3761]34
[3775]35    @Autowired
36    private JpaComponentDao jpaComponentDao;
[3863]37
[3785]38    @Autowired
[3863]39    private CommentsDao commentsDao;
40
41    @Autowired
[3785]42    private UserDao userDao;
[3775]43
[3449]44    private final static Logger LOG = LoggerFactory
[3775]45            .getLogger(ComponentDaoImpl.class);
[1300]46
[3775]47    /**
[3863]48     * Copy a set of properties which are allowed to change from "from" to "to"
49     * thus making sure that only those properties are updated and not other
50     * ones.
51     *
[3775]52     * @param from
53     * @param to
54     */
[3897]55    protected void copyPersistentProperties(BaseDescription from, BaseDescription to) {
[3775]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());
63    }
64
[3761]65    protected String getTableName() {
66        return "persistentcomponents";
67    };
[1305]68
[3775]69    private boolean compare(String s, Number n) {
70        return ("" + s).equals("" + n);
[3761]71    }
[1701]72
[3775]73    private String toString(Number n) {
74        if (n == null)
75            return null;
76        return n.toString();
[1303]77    }
[1305]78
[3897]79    private BaseDescription castDown(BaseDescription c) {
[3775]80        // JPA will save only BaseComponent instances, not derived classes
81        if (c instanceof ComponentDescription
82                || c instanceof ProfileDescription) {
[3897]83            BaseDescription copy = new BaseDescription();
[3775]84            ComponentUtils.copyPropertiesFrom(c, copy);
85            return copy;
[3770]86        }
[3775]87        return c;
[3770]88    }
89
[3775]90    /**
[3897]91     * Update {@link BaseDescription#getCommentsCount()} (which is a transient
[3863]92     * property) with the number of comments for that component and load
[3897]93     * {@link BaseDescription#getContent()}
[3863]94     *
[3897]95     * @param baseDescription
[3775]96     */
[3897]97    private void augment(BaseDescription baseDescription) {
98        if (baseDescription == null)
[3775]99            return;
[3897]100        int count = (int) commentsDao.findCommentCountForComponent(baseDescription
[3863]101                .getId());
[3897]102        baseDescription.setCommentsCount(count);
103        baseDescription.setContent(jpaComponentDao.findContentByComponentId(baseDescription
[3863]104                .getId()));
[3770]105    }
106
[3863]107/**
[3897]108     * Update {@link BaseDescription#getCommentsCount()
109     * @param baseDescription
[3863]110     */
[3897]111    private List<BaseDescription> augment(List<BaseDescription> baseDescription) {
[5549]112        if (baseDescription.isEmpty()) {
[3897]113            return baseDescription;
[5549]114        }
[3897]115        Map<String, BaseDescription> map = new HashMap<String, BaseDescription>();
[3863]116        List<String> idlist = new ArrayList<String>();
[3897]117        for (BaseDescription c:baseDescription) {
[3863]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        }
[3897]128        return baseDescription;
[3770]129    }
[3775]130
[1349]131    /**
[1357]132     *
133     * @param cmdId
134     *            CMD id
[1691]135     * @return Whether the specified item is in the public space
[1349]136     */
[3449]137    @Override
[1348]138    public boolean isPublic(String cmdId) {
[3897]139        BaseDescription baseDescription = jpaComponentDao.findByComponentId(cmdId);
140        return baseDescription != null && baseDescription.isPublic();
[1346]141    }
142
[1349]143    /**
[1357]144     *
145     * @param cmdId
146     *            CMD id
147     * @param userId
148     *            User db id of workspace owner
[1691]149     * @return Whether the specified item is in the specified user's workspace
[1349]150     */
[3449]151    @Override
[1348]152    public boolean isInUserSpace(String cmdId, Number userId) {
[3897]153        BaseDescription baseDescription = jpaComponentDao.findByComponentId(cmdId);
154        boolean b = baseDescription != null && !baseDescription.isPublic()
155                && compare(baseDescription.getUserId(), userId);
[3775]156        return b;
[1346]157    }
158
[1303]159    /**
[1298]160     *
[1357]161     * @param cmdId
[1691]162     *            CMD id
163     * @param userId
164     *            User db id of workspace owner, null for public registry
[3449]165     * @return Whether the specified item is in the specified workspace (user or
166     *         public)
[1691]167     */
[5549]168//    @Override
169//    public boolean isAccessible(String cmdId, Number userId) {
170//      if (userId == null) {
171//          return this.isPublic(cmdId);
172//      } else {
173//          return this.isInUserSpace(cmdId, userId);
174//      }
175//    }
[1691]176
177    /**
178     *
179     * @param cmdId
[1357]180     *            Profile or component Id (not primary key)
[1298]181     * @return String value of XML content for profile or component
182     */
[3449]183    @Override
[3863]184    public String getContent(boolean isDeleted, String cmdId) {
185        return jpaComponentDao.findContentByComponentId(cmdId, isDeleted);
[1297]186    }
187
[1298]188    /**
[1357]189     * @param description
190     *            Description to insert
191     * @param content
192     *            Content to insert and refer to from description
[1298]193     * @return Id of newly inserted description
194     */
[3449]195    @Override
[3897]196    public Number insertDescription(BaseDescription description, String content,
[3863]197            boolean isPublic, Number userId) {
[1357]198
[3775]199        if (description.getId() != null
200                && jpaComponentDao.findByComponentId(description.getId()) != null) {
201            throw new IllegalArgumentException("Component "
202                    + description.getId() + " already in DB");
[1337]203        }
[3863]204        if (content == null)
205            content = "";
[3897]206        BaseDescription copy = castDown(description);
[3775]207        copy.setContent(content);
208        copy.setPublic(isPublic);
209        copy.setUserId(toString(userId));
[5549]210        copy.setCreatorName(description.getCreatorName());
[3775]211        copy = jpaComponentDao.saveAndFlush(copy);
[3863]212        jpaComponentDao.updateContent(copy.getId(), content);
[3775]213        return copy.getDbId();
[1337]214    }
215
[1303]216    /**
[1331]217     * Updates a description by database id
[1357]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)
[1331]225     */
[3449]226    @Override
[3897]227    public void updateDescription(Number id, BaseDescription description,
[3449]228            String content) {
[3863]229        if (content == null)
230            content = "";
[3449]231        if (description != null) {
232            // Update description
[3897]233            BaseDescription copy = jpaComponentDao.findByComponentId(description
[3775]234                    .getId());
235            copyPersistentProperties(description, copy);
236            jpaComponentDao.saveAndFlush(copy);
[3863]237            jpaComponentDao.updateContent(copy.getId(), content);
[3449]238        }
[1331]239
[3449]240        if (content != null) {
[3897]241            BaseDescription copy = jpaComponentDao.findOne(id.longValue());
[3775]242            copy.setContent(content);
243            jpaComponentDao.saveAndFlush(copy);
[3863]244            jpaComponentDao.updateContent(copy.getId(), content);
[1331]245        }
246    }
247
248    /**
[1305]249     * Retrieves description by it's primary key Id
[1357]250     *
251     * @param id
252     *            Description key
[1305]253     * @return The description, if it exists; null otherwise
254     */
[3449]255    @Override
[3897]256    public BaseDescription getById(Number id) {
257        BaseDescription baseDescription = jpaComponentDao.findOne(id.longValue());
258        if (baseDescription != null && !baseDescription.isDeleted()) {
259            augment(baseDescription);
260            return baseDescription;
[3775]261        }
262        return null;
[1305]263    }
264
265    /**
[1346]266     * Get by ComponentId / ProfileId, whether in userspace or public
[1357]267     *
268     * @param id
269     *            Full component id
[1309]270     * @return The description, if it exists; null otherwise
271     */
[3449]272    @Override
[3897]273    public BaseDescription getByCmdId(String id) {
274        BaseDescription baseDescription = jpaComponentDao.findByComponentId(id);
275        if (baseDescription != null && !baseDescription.isDeleted()) {
276            augment(baseDescription);
277            return baseDescription;
[3775]278        }
279        return null;
[1309]280    }
281
[5549]282 
[1309]283    /**
[1357]284     *
285     * @param cmdId
286     *            CMD Id of description
[1331]287     * @return Database id for description record
288     */
[3449]289    @Override
[1331]290    public Number getDbId(String cmdId) {
[3775]291        Number id = null;
[3897]292        BaseDescription c = jpaComponentDao.findByComponentId(cmdId);
[3863]293        if (c != null)
[3775]294            id = c.getDbId();
295        return id;
[1331]296    }
297
298    /**
[1357]299     *
[3863]300     * @return All descriptions in the public space. Won't include xml content
301     *         and comments count.
[1303]302     */
[3449]303    @Override
[3897]304    public List<BaseDescription> getPublicDescriptions() {
[3863]305        return augment(jpaComponentDao.findPublicItems());
[1303]306    }
[1300]307
[1305]308    /**
[3449]309     * @return List of deleted descriptions in user space or in public when
[3863]310     *         userId=null. Won't include xml content and comments count.
[1357]311     * @param userId
312     */
[3449]313    @Override
[3897]314    public List<BaseDescription> getDeletedDescriptions(Number userId) {
315        List<BaseDescription> list = null;
[1357]316        if (userId != null) {
[3863]317            list = jpaComponentDao.findDeletedItemsForUser(userId.longValue());
[1357]318        } else {
[3863]319            list = jpaComponentDao.findPublicDeletedItems();
[1357]320        }
[3863]321        list = augment(list);
322        return list;
[1357]323    }
324
325    /**
326     *
[3863]327     * @return All the user's descriptions not in the public space .Won't
328     *         include xml content and comments count.
[1305]329     */
[3449]330    @Override
[5549]331    public List<BaseDescription> getPrivateBaseDescriptions(Number userId, String prefix) {
332        return augment(jpaComponentDao.findItemsForUserThatAreNotInGroups(userId.longValue(),prefix + "%"));
333        //return augment(jpaComponentDao.findNotPublishedUserItems(userId.longValue(),prefix + "%"));
[1305]334    }
[3775]335
[5549]336   
[3763]337    @Override
[3897]338    public void setDeleted(BaseDescription desc, boolean isDeleted) {
339        BaseDescription copy = jpaComponentDao.findByComponentId(desc.getId());
[3775]340        copy.setDeleted(isDeleted);
341        jpaComponentDao.saveAndFlush(copy);
[1316]342    }
343
[3449]344    @Override
[1331]345    public void setPublished(Number id, boolean published) {
[3897]346        BaseDescription copy = jpaComponentDao.findOne(id.longValue());
[3775]347        copy.setPublic(published);
348        jpaComponentDao.saveAndFlush(copy);
[1326]349    }
[1318]350
[1331]351    /**
[1357]352     *
353     * @param id
354     *            Id of description record
[1331]355     * @return Principal name of description's owner, if any. Otherwise, null.
356     */
[3449]357    @Override
[1331]358    public String getOwnerPrincipalName(Number id) {
[3897]359        BaseDescription baseDescription = getById(id);
360        if (baseDescription == null)
[1331]361            return null;
[3863]362
[3897]363        long userId = baseDescription.getDbUserId();
[3785]364        RegistryUser user = userDao.findOne(userId);
365        if (user == null)
366            return null;
367
368        return user.getPrincipalName();
[1331]369    }
370
[3761]371    @Override
[5549]372    public List<BaseDescription> getPublicBaseDescriptions(String prefix) {
373        return augment(jpaComponentDao.findPublishedItems(prefix + "%"));
[3761]374    }
375
[5549]376   
[3761]377    @Override
[3897]378    public List<String> getAllNonDeletedProfileIds() {
379        return jpaComponentDao.findNonDeletedItemIds(ProfileDescription.PROFILE_PREFIX+"%");
380    }
381
382    @Override
383    public List<String> getAllNonDeletedComponentIds() {
384        return jpaComponentDao.findNonDeletedItemIds(ComponentDescription.COMPONENT_PREFIX+"%");
385    }
[5549]386   
387    @Override
388    public List<BaseDescription> getAllNonDeletedDescriptions() {
389        return jpaComponentDao.findNonDeletedDescriptions();
390    }
391   
392    // Olha was here
393    @Override
394    public List<String> getAllItemIdsInGroup(String prefix, Long groupId) {
395        // we are ineterested only in non-published components in the group
396        return jpaComponentDao.findAllItemIdsInGroup(false, prefix + "%", groupId);
397    }
398   
399   
[1297]400}
Note: See TracBrowser for help on using the repository browser.