package com.elasticinbox.rest.v2;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import com.elasticinbox.core.DAOFactory;
import com.elasticinbox.core.LabelDAO;
import com.elasticinbox.core.MessageDAO;
import com.elasticinbox.core.model.Labels;
import com.elasticinbox.core.model.Mailbox;

/**
 * This JAX-RS resource is responsible for maintaining mailbox consitency
 * through scrub operation.
 * 
 * @author Rustam Aliyev
 */
@Path("{domain}/{user}/mailbox/scrub")
public class ScrubResource
{
	private final MessageDAO messageDAO;
	private final LabelDAO labelDAO;

	@Context UriInfo uriInfo;

	public ScrubResource() {
		DAOFactory dao = DAOFactory.getDAOFactory();
		messageDAO = dao.getMessageDAO();
		labelDAO = dao.getLabelDAO();
	}

	/**
	 * Initialize new account
	 * 
	 * @param account
	 * @return
	 */
	@POST
	@Path("counters")
	@Produces(MediaType.APPLICATION_JSON)
	public Response scrubIndex(
			@PathParam("user") final String user,
			@PathParam("domain") final String domain)
	{
		Mailbox mailbox = new Mailbox(user, domain);
		
		Labels calculatedCounters = messageDAO.calculateCounters(mailbox);
		labelDAO.setCounters(mailbox, calculatedCounters);
		
		return Response.noContent().build();
	}

}

@@ -1,8 +1,1611 @@
+/**%0A * Copyright (c) 2011-2012 Optimax Software Ltd.%0A * All rights reserved.%0A * %0A * Redistribution and use in source and binary forms, with or without%0A * modification, are permitted provided that the following conditions are met:%0A * %0A *  * Redistributions of source code must retain the above copyright notice,%0A *    this list of conditions and the following disclaimer.%0A *  * Redistributions in binary form must reproduce the above copyright notice,%0A *    this list of conditions and the following disclaimer in the documentation%0A *    and/or other materials provided with the distribution.%0A *  * Neither the name of Optimax Software, ElasticInbox, nor the names%0A *    of its contributors may be used to endorse or promote products derived%0A *    from this software without specific prior written permission.%0A * %0A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22%0A * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE%0A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE%0A * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE%0A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL%0A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR%0A * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER%0A * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,%0A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE%0A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%0A */%0A%0A
 package 
@@ -2551,30 +2551,46 @@
 %09 * 
-Initialize new ac
+Scrub mailbox and recalculate 
 count
+ers
 %0A%09 *

add copyright header, comment