/*
 * Copyright (c) 2013 Lucas Holt
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

package com.justjournal.db;

import org.apache.cayenne.Cayenne;
import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.access.DataContext;
import org.apache.log4j.Logger;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Lucas Holt
 */
public class TagDao {

    private static final Logger log = Logger.getLogger(TagDao.class);

    public static Tag viewSingle(int id) {
        Tag tag = null;

        try {
            ObjectContext dataContext = DataContext.getThreadObjectContext();

            com.justjournal.model.Tags tagItem =
                    Cayenne.objectForPK(dataContext, com.justjournal.model.Tags.class, id);
            tag = new Tag(id, tagItem.getName());
            tag.setCount(tagItem.getTagsToEntryList().size());


        } catch (Exception e1) {
            log.error(e1);
        }

        return tag;
    }

    public static Collection<Tag> list() {
        Collection<Tag> tags = new ArrayList<Tag>();
        try {
            // TODO: Convert to Cayenne
            ResultSet rs = SQLHelper.executeResultSet("select tags.id, tags.name as name, count(*) as count from entry_tags, tags where tags.id=entry_tags.tagid GROUP by tags.name;");

            while (rs.next()) {
                Tag tag = new Tag(rs.getInt(1), rs.getString(2));
                tag.setCount(rs.getInt(3));
                tags.add(tag);
            }
            rs.close();
        } catch (Exception ex) {
            log.error(ex);
        }
        return tags;
    }
}

@@ -1618,16 +1618,34 @@
 n;%0A%0A/**%0A
+ * Tag management%0A
  * @auth
@@ -1662,16 +1662,22 @@
 olt%0A */%0A
+final 
 public c
@@ -1758,24 +1758,113 @@
 ao.class);%0A%0A
+    /**%0A     * Get a single tag by id%0A     * @param id tag id%0A     * @return tag%0A     */%0A
     public s
@@ -2266,18 +2266,16 @@
 ize());%0A
-%0A%0A
         
@@ -2360,24 +2360,91 @@
 tag;%0A    %7D%0A%0A
+    /**%0A     * List all tags%0A     * @return tag collection%0A     */%0A
     public s

add more comments