001: /*
002: * $Id: AbstractCRUDAction.java 476710 2006-11-19 05:05:14Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.showcase.action;
022:
023: import java.io.Serializable;
024: import java.util.Collection;
025:
026: import org.apache.log4j.Logger;
027: import org.apache.struts2.showcase.dao.Dao;
028: import org.apache.struts2.showcase.model.IdEntity;
029:
030: import com.opensymphony.xwork2.ActionSupport;
031:
032: /**
033: * AbstractCRUDAction.
034: *
035: */
036:
037: public abstract class AbstractCRUDAction extends ActionSupport {
038:
039: private static final Logger log = Logger
040: .getLogger(AbstractCRUDAction.class);
041:
042: private Collection availableItems;
043: private String[] toDelete;
044:
045: protected abstract Dao getDao();
046:
047: public Collection getAvailableItems() {
048: return availableItems;
049: }
050:
051: public String[] getToDelete() {
052: return toDelete;
053: }
054:
055: public void setToDelete(String[] toDelete) {
056: this .toDelete = toDelete;
057: }
058:
059: public String list() throws Exception {
060: this .availableItems = getDao().findAll();
061: if (log.isDebugEnabled()) {
062: log.debug("AbstractCRUDAction - [list]: "
063: + (availableItems != null ? ""
064: + availableItems.size() : "no")
065: + " items found");
066: }
067: return execute();
068: }
069:
070: public String delete() throws Exception {
071: if (toDelete != null) {
072: int count = 0;
073: for (int i = 0, j = toDelete.length; i < j; i++) {
074: count = count + getDao().delete(toDelete[i]);
075: }
076: if (log.isDebugEnabled()) {
077: log.debug("AbstractCRUDAction - [delete]: " + count
078: + " items deleted.");
079: }
080: }
081: return SUCCESS;
082: }
083:
084: /**
085: * Utility method for fetching already persistent object from storage for usage in params-prepare-params cycle.
086: *
087: * @param tryId The id to try to get persistent object for
088: * @param tryObject The object, induced by first params invocation, possibly containing id to try to get persistent
089: * object for
090: * @return The persistent object, if found. <tt>null</tt> otherwise.
091: */
092: protected IdEntity fetch(Serializable tryId, IdEntity tryObject) {
093: IdEntity result = null;
094: if (tryId != null) {
095: result = getDao().get(tryId);
096: } else if (tryObject != null) {
097: result = getDao().get(tryObject.getId());
098: }
099: return result;
100: }
101: }
|