001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
019    import com.liferay.portal.kernel.dao.orm.Projection;
020    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
021    import com.liferay.portal.kernel.dao.orm.Property;
022    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portlet.documentlibrary.model.DLSyncEvent;
025    import com.liferay.portlet.documentlibrary.service.base.DLSyncEventLocalServiceBaseImpl;
026    
027    import java.util.List;
028    
029    /**
030     * @author Dennis Ju
031     */
032    public class DLSyncEventLocalServiceImpl
033            extends DLSyncEventLocalServiceBaseImpl {
034    
035            @Override
036            public DLSyncEvent addDLSyncEvent(String event, String type, long typePK)
037                    throws SystemException {
038    
039                    DLSyncEvent dlSyncEvent = dlSyncEventPersistence.fetchByTypePK(typePK);
040    
041                    if (dlSyncEvent == null) {
042                            long dlSyncEventId = counterLocalService.increment();
043    
044                            dlSyncEvent = dlSyncEventPersistence.create(dlSyncEventId);
045    
046                            dlSyncEvent.setType(type);
047                            dlSyncEvent.setTypePK(typePK);
048                    }
049    
050                    dlSyncEvent.setModifiedTime(System.currentTimeMillis());
051                    dlSyncEvent.setEvent(event);
052    
053                    return dlSyncEventPersistence.update(dlSyncEvent);
054            }
055    
056            @Override
057            public void deleteDLSyncEvents() throws SystemException {
058                    dlSyncEventPersistence.removeAll();
059            }
060    
061            @Override
062            public List<DLSyncEvent> getDLSyncEvents(long modifiedTime)
063                    throws SystemException {
064    
065                    return dlSyncEventPersistence.findByModifiedTime(modifiedTime);
066            }
067    
068            @Override
069            public List<DLSyncEvent> getLatestDLSyncEvents() throws SystemException {
070                    DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
071                            DLSyncEvent.class);
072    
073                    Property property = PropertyFactoryUtil.forName("modifiedTime");
074    
075                    DynamicQuery modifiedTimeDynamicQuery =
076                            DynamicQueryFactoryUtil.forClass(DLSyncEvent.class);
077    
078                    Projection projection = ProjectionFactoryUtil.max("modifiedTime");
079    
080                    modifiedTimeDynamicQuery.setProjection(projection);
081    
082                    dynamicQuery.add(property.eq(modifiedTimeDynamicQuery));
083    
084                    return dlSyncEventPersistence.findWithDynamicQuery(dynamicQuery);
085            }
086    
087    }