alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Struts example source code file (AbstractCRUDAction.java)

This example Struts source code file (AbstractCRUDAction.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Struts tags/keywords

abstractcrudaction, abstractcrudaction, actionsupport, collection, dao, exception, exception, identity, identity, io, logger, string, string, success, util

The Struts AbstractCRUDAction.java source code

/*
 * $Id: AbstractCRUDAction.java 476710 2006-11-19 05:05:14Z mrdon $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.struts2.showcase.action;

import java.io.Serializable;
import java.util.Collection;

import org.apache.log4j.Logger;
import org.apache.struts2.showcase.dao.Dao;
import org.apache.struts2.showcase.model.IdEntity;

import com.opensymphony.xwork2.ActionSupport;

/**
 * AbstractCRUDAction.
 *
 */

public abstract class AbstractCRUDAction extends ActionSupport {

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

    private Collection availableItems;
    private String[] toDelete;

    protected abstract Dao getDao();


    public Collection getAvailableItems() {
        return availableItems;
    }

    public String[] getToDelete() {
        return toDelete;
    }

    public void setToDelete(String[] toDelete) {
        this.toDelete = toDelete;
    }

    public String list() throws Exception {
        this.availableItems = getDao().findAll();
        if (log.isDebugEnabled()) {
            log.debug("AbstractCRUDAction - [list]: " + (availableItems !=null?""+availableItems.size():"no") + " items found");
        }
        return execute();
    }

    public String delete() throws Exception {
        if (toDelete != null) {
            int count=0;
            for (int i = 0, j=toDelete.length; i < j; i++) {
                count = count + getDao().delete(toDelete[i]);
            }
            if (log.isDebugEnabled()) {
                log.debug("AbstractCRUDAction - [delete]: " + count + " items deleted.");
            }
        }
        return SUCCESS;
    }

    /**
     * Utility method for fetching already persistent object from storage for usage in params-prepare-params cycle.
     *
     * @param tryId     The id to try to get persistent object for
     * @param tryObject The object, induced by first params invocation, possibly containing id to try to get persistent
     *                  object for
     * @return The persistent object, if found. <tt>null otherwise.
     */
    protected IdEntity fetch(Serializable tryId, IdEntity tryObject) {
        IdEntity result = null;
        if (tryId != null) {
            result = getDao().get(tryId);
        } else if (tryObject != null) {
            result = getDao().get(tryObject.getId());
        }
        return result;
    }
}

Other Struts examples (source code examples)

Here is a short list of links related to this Struts AbstractCRUDAction.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.