Project

General

Profile

1 4399 leinfelder
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2008 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 * Author: Benjamin Leinfelder
7
 * '$Date:  $'
8
 * '$Revision:  $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24
25
package edu.ucsb.nceas.metacat.cart;
26
27
import java.util.Map;
28
29
import org.apache.commons.logging.Log;
30
import org.apache.commons.logging.LogFactory;
31
32
import edu.ucsb.nceas.metacat.service.SessionService;
33
34
35
/**
36
 * Class to query data
37
 */
38
public class CartManager {
39
40
	private static Log log = LogFactory.getLog(CartManager.class);
41
42
	private String sessionId = null;
43
	private DocumentCart documentCart = null;
44
45
	/**
46
	 * empty constructor to initialize query
47
	 */
48
	public CartManager() {
49
		init();
50
	}
51
52
	public CartManager(String sessionId) {
53
		// initialize the necessary parts
54
		this.sessionId = sessionId;
55
		init();
56
	}
57
58
	private void init() {
59
		documentCart = SessionService.getRegisteredSession(sessionId).getDocumentCart();
60
		if (documentCart == null) {
61
			documentCart = new DocumentCart();
62
		}
63
64
	}
65
66
	public void editCart(String operation, String[] docids, Map fields) {
67
68
		//for attribute fields
69
		if (operation.equalsIgnoreCase("addField")) {
70
			documentCart.addFields(fields);
71
		}
72
		if (operation.equalsIgnoreCase("removeField")) {
73
			String field = (String) fields.keySet().toArray()[0];
74
			documentCart.removeField(field);
75
		}
76
		if (operation.equalsIgnoreCase("addFields")) {
77
			documentCart.addFields(fields);
78
		}
79
		if (operation.equalsIgnoreCase("clearfields")) {
80
			documentCart.clearFields();
81
		}
82
83
		//for document ids
84
		if (operation.equalsIgnoreCase("clear")) {
85
			documentCart.clear();
86
		}
87
		if (docids != null) {
88
			for (int i=0; i < docids.length; i++) {
89
				if (operation.equalsIgnoreCase("add")) {
90
					documentCart.addDocument(docids[i], fields);
91
				}
92
				if (operation.equalsIgnoreCase("remove")) {
93
					documentCart.removeDocument(docids[i]);
94
				}
95
			}
96
		}
97
98
		SessionService.getRegisteredSession(sessionId).setDocumentCart(documentCart);
99
	}
100
101
}