Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 771 | blopes | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
||
| 3 | * contributor license agreements. See the NOTICE file distributed with |
||
| 4 | * this work for additional information regarding copyright ownership. |
||
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
||
| 6 | * (the "License"); you may not use this file except in compliance with |
||
| 7 | * the License. You may obtain a copy of the License at |
||
| 8 | * |
||
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
| 10 | * |
||
| 11 | * Unless required by applicable law or agreed to in writing, software |
||
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
| 14 | * See the License for the specific language governing permissions and |
||
| 15 | * limitations under the License. |
||
| 16 | */ |
||
| 17 | package sessions; |
||
| 18 | |||
| 19 | import java.io.Serializable; |
||
| 20 | import java.util.Collections; |
||
| 21 | import java.util.HashSet; |
||
| 22 | import java.util.Set; |
||
| 23 | |||
| 24 | public class DummyCart implements Serializable { |
||
| 25 | |||
| 26 | private static final long serialVersionUID = 1L; |
||
| 27 | |||
| 28 | final Set<Item> items = Collections.synchronizedSet(new HashSet<>()); |
||
| 29 | int itemId = -1; |
||
| 30 | String submit = null; |
||
| 31 | |||
| 32 | public void setItemId(int itemId) { |
||
| 33 | this.itemId = itemId; |
||
| 34 | } |
||
| 35 | |||
| 36 | public void setSubmit(String s) { |
||
| 37 | submit = s; |
||
| 38 | } |
||
| 39 | |||
| 40 | private void addItem(int itemId) { |
||
| 41 | try { |
||
| 42 | items.add(Item.values()[itemId]); |
||
| 43 | } catch (ArrayIndexOutOfBoundsException e) { |
||
| 44 | // Ignore. Can only happen if user edits URL directly. |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | private void removeItem(int itemId) { |
||
| 49 | try { |
||
| 50 | items.remove(Item.values()[itemId]); |
||
| 51 | } catch (ArrayIndexOutOfBoundsException e) { |
||
| 52 | // Ignore. Can only happen if user edits URL directly. |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | public Item[] getItems() { |
||
| 57 | return items.toArray(new Item[0]); |
||
| 58 | } |
||
| 59 | |||
| 60 | public void processRequest() { |
||
| 61 | // null value for submit - user hit enter instead of clicking on |
||
| 62 | // "add" or "remove" |
||
| 63 | if (submit == null || submit.equals("add")) { |
||
| 64 | addItem(itemId); |
||
| 65 | } else if (submit.equals("remove")) { |
||
| 66 | removeItem(itemId); |
||
| 67 | } |
||
| 68 | |||
| 69 | // reset at the end of the request |
||
| 70 | reset(); |
||
| 71 | } |
||
| 72 | |||
| 73 | // reset |
||
| 74 | private void reset() { |
||
| 75 | submit = null; |
||
| 76 | itemId = -1; |
||
| 77 | } |
||
| 78 | } |