001/**
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.isis.objectstore.jdo.applib.service.command;
018
019import java.sql.Timestamp;
020import java.util.List;
021import java.util.UUID;
022
023import org.joda.time.LocalDate;
024
025import org.apache.isis.applib.AbstractFactoryAndRepository;
026import org.apache.isis.applib.annotation.DomainService;
027import org.apache.isis.applib.annotation.Programmatic;
028import org.apache.isis.applib.query.Query;
029import org.apache.isis.applib.query.QueryDefault;
030import org.apache.isis.applib.services.bookmark.Bookmark;
031import org.apache.isis.applib.services.command.Command;
032import org.apache.isis.applib.services.command.CommandContext;
033
034
035/**
036 * Provides supporting functionality for querying and persisting
037 * {@link org.apache.isis.objectstore.jdo.applib.service.command.CommandJdo command} entities.
038 *
039 * <p>
040 * This supporting service with no UI and no side-effects, and is there are no other implementations of the service,
041 * thus has been annotated with {@link org.apache.isis.applib.annotation.DomainService}.  This means that there is no
042 * need to explicitly register it as a service (eg in <tt>isis.properties</tt>).
043 */
044@DomainService
045public class CommandServiceJdoRepository extends AbstractFactoryAndRepository {
046
047    @Programmatic
048    public List<CommandJdo> findByFromAndTo(
049            final LocalDate from, final LocalDate to) {
050        final Timestamp fromTs = toTimestampStartOfDayWithOffset(from, 0);
051        final Timestamp toTs = toTimestampStartOfDayWithOffset(to, 1);
052        
053        final Query<CommandJdo> query;
054        if(from != null) {
055            if(to != null) {
056                query = new QueryDefault<CommandJdo>(CommandJdo.class, 
057                        "findByTimestampBetween", 
058                        "from", fromTs,
059                        "to", toTs);
060            } else {
061                query = new QueryDefault<CommandJdo>(CommandJdo.class, 
062                        "findByTimestampAfter", 
063                        "from", fromTs);
064            }
065        } else {
066            if(to != null) {
067                query = new QueryDefault<CommandJdo>(CommandJdo.class, 
068                        "findByTimestampBefore", 
069                        "to", toTs);
070            } else {
071                query = new QueryDefault<CommandJdo>(CommandJdo.class, 
072                        "find");
073            }
074        }
075        return allMatches(query);
076    }
077
078
079    @Programmatic
080    public CommandJdo findByTransactionId(final UUID transactionId) {
081        persistCurrentCommandIfRequired();
082        return firstMatch(
083                new QueryDefault<CommandJdo>(CommandJdo.class, 
084                        "findByTransactionId", 
085                        "transactionId", transactionId));
086    }
087
088    @Programmatic
089    public List<CommandJdo> findCurrent() {
090        persistCurrentCommandIfRequired();
091        return allMatches(
092                new QueryDefault<CommandJdo>(CommandJdo.class, "findCurrent"));
093    }
094    
095    @Programmatic
096    public List<CommandJdo> findCompleted() {
097        persistCurrentCommandIfRequired();
098        return allMatches(
099                new QueryDefault<CommandJdo>(CommandJdo.class, "findCompleted"));
100    }
101
102    private void persistCurrentCommandIfRequired() {
103        if(commandContext == null || commandService == null) {
104            return;
105        } 
106        final Command command = commandContext.getCommand();
107        final CommandJdo commandJdo = commandService.asUserInitiatedCommandJdo(command);
108        if(commandJdo == null) {
109            return;
110        } 
111        persistIfNotAlready(commandJdo);
112    }
113
114    // //////////////////////////////////////
115
116    
117    @Programmatic
118    public List<CommandJdo> findByTargetAndFromAndTo(
119            final Bookmark target, final LocalDate from, final LocalDate to) {
120        final String targetStr = target.toString();
121        final Timestamp fromTs = toTimestampStartOfDayWithOffset(from, 0);
122        final Timestamp toTs = toTimestampStartOfDayWithOffset(to, 1);
123        
124        final Query<CommandJdo> query;
125        if(from != null) {
126            if(to != null) {
127                query = new QueryDefault<CommandJdo>(CommandJdo.class, 
128                        "findByTargetAndTimestampBetween", 
129                        "targetStr", targetStr,
130                        "from", fromTs,
131                        "to", toTs);
132            } else {
133                query = new QueryDefault<CommandJdo>(CommandJdo.class, 
134                        "findByTargetAndTimestampAfter", 
135                        "targetStr", targetStr,
136                        "from", fromTs);
137            }
138        } else {
139            if(to != null) {
140                query = new QueryDefault<CommandJdo>(CommandJdo.class, 
141                        "findByTargetAndTimestampBefore", 
142                        "targetStr", targetStr,
143                        "to", toTs);
144            } else {
145                query = new QueryDefault<CommandJdo>(CommandJdo.class, 
146                        "findByTarget", 
147                        "targetStr", targetStr);
148            }
149        }
150        return allMatches(query);
151    }
152
153    private static Timestamp toTimestampStartOfDayWithOffset(final LocalDate dt, int daysOffset) {
154        return dt!=null
155                ?new java.sql.Timestamp(dt.toDateTimeAtStartOfDay().plusDays(daysOffset).getMillis())
156                :null;
157    }
158
159    // //////////////////////////////////////
160
161    
162    @javax.inject.Inject
163    private CommandServiceJdo commandService;
164    
165    @javax.inject.Inject
166    private CommandContext commandContext;
167
168
169}