mirror of https://github.com/tasks/tasks
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
3.8 KiB
Java
140 lines
3.8 KiB
Java
/**
|
|
* Copyright (c) 2012 Todoroo Inc
|
|
*
|
|
* See the file "LICENSE" for the full license governing this code.
|
|
*/
|
|
package com.todoroo.astrid.dao;
|
|
|
|
import com.todoroo.andlib.data.DatabaseDao;
|
|
import com.todoroo.andlib.sql.Criterion;
|
|
import com.todoroo.andlib.sql.Join;
|
|
import com.todoroo.andlib.sql.Query;
|
|
import com.todoroo.andlib.utility.DateUtilities;
|
|
import com.todoroo.astrid.data.Metadata;
|
|
import com.todoroo.astrid.data.Task;
|
|
|
|
import org.tasks.injection.ApplicationScope;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
/**
|
|
* Data Access layer for {@link Metadata}-related operations.
|
|
*
|
|
* @author Tim Su <tim@todoroo.com>
|
|
*
|
|
*/
|
|
@ApplicationScope
|
|
public class MetadataDao {
|
|
|
|
private final DatabaseDao<Metadata> dao;
|
|
|
|
@Inject
|
|
public MetadataDao(Database database) {
|
|
dao = new DatabaseDao<>(database, Metadata.class);
|
|
}
|
|
|
|
public Metadata getFirst(Query query) {
|
|
return dao.getFirst(query);
|
|
}
|
|
|
|
public Metadata getFirstActiveByTaskAndKey(long taskId, String key) {
|
|
return getFirst(Query.select(Metadata.PROPERTIES).where(Criterion.and(
|
|
MetadataCriteria.byTaskAndwithKey(taskId, key),
|
|
MetadataCriteria.isActive())));
|
|
}
|
|
|
|
public int update(Criterion where, Metadata template) {
|
|
return dao.update(where, template);
|
|
}
|
|
|
|
public void createNew(Metadata metadata) {
|
|
dao.createNew(metadata);
|
|
}
|
|
|
|
public List<Metadata> toList(Criterion criterion) {
|
|
return toList(Query.select(Metadata.PROPERTIES).where(criterion));
|
|
}
|
|
|
|
public List<Metadata> toList(Query where) {
|
|
return dao.toList(where);
|
|
}
|
|
|
|
public int deleteWhere(Criterion criterion) {
|
|
return dao.deleteWhere(criterion);
|
|
}
|
|
|
|
public boolean delete(long id) {
|
|
return dao.delete(id);
|
|
}
|
|
|
|
public void saveExisting(Metadata metadata) {
|
|
dao.saveExisting(metadata);
|
|
}
|
|
|
|
public Metadata fetch(long id) {
|
|
return dao.fetch(id, Metadata.PROPERTIES);
|
|
}
|
|
|
|
// --- SQL clause generators
|
|
|
|
/**
|
|
* Generates SQL clauses
|
|
*/
|
|
public static class MetadataCriteria {
|
|
|
|
/** Returns all metadata associated with a given task */
|
|
public static Criterion byTask(long taskId) {
|
|
return Metadata.TASK.eq(taskId);
|
|
}
|
|
|
|
/** Returns all metadata associated with a given key */
|
|
public static Criterion withKey(String key) {
|
|
return Metadata.KEY.eq(key);
|
|
}
|
|
|
|
/** Returns all metadata associated with a given key */
|
|
public static Criterion byTaskAndwithKey(long taskId, String key) {
|
|
return Criterion.and(withKey(key), byTask(taskId));
|
|
}
|
|
|
|
public static Criterion isActive() {
|
|
return Metadata.DELETION_DATE.eq(0);
|
|
}
|
|
|
|
public static Criterion isDeleted() {
|
|
return Metadata.DELETION_DATE.gt(0);
|
|
}
|
|
}
|
|
|
|
public boolean persist(Metadata item) {
|
|
if(!item.containsNonNullValue(Metadata.TASK)) {
|
|
throw new IllegalArgumentException("metadata needs to be attached to a task: " + item.getMergedValues()); //$NON-NLS-1$
|
|
}
|
|
if(!item.containsValue(Metadata.CREATION_DATE)) {
|
|
item.setCreationDate(DateUtilities.now());
|
|
}
|
|
|
|
return dao.persist(item);
|
|
}
|
|
|
|
/**
|
|
* Clean up metadata. Typically called on startup
|
|
*/
|
|
public void removeDanglingMetadata() {
|
|
dao.deleteWhere(Metadata.ID.in(Query.select(Metadata.ID).from(Metadata.TABLE).join(Join.left(Task.TABLE,
|
|
Metadata.TASK.eq(Task.ID))).where(Task.TITLE.isNull())));
|
|
}
|
|
|
|
public List<Metadata> byTask(long taskId) {
|
|
return toList(MetadataCriteria.byTask(taskId));
|
|
}
|
|
|
|
public List<Metadata> byTaskAndKey(long taskId, String key) {
|
|
return dao.toList(Query.select(Metadata.PROPERTIES).where(
|
|
Criterion.and(Metadata.TASK.eq(taskId), Metadata.KEY.eq(key))));
|
|
}
|
|
}
|
|
|