<commit_before>//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.presents.util;

import com.samskivert.io.PersistenceException;
import com.samskivert.util.Invoker;

import com.threerings.presents.Log;
import com.threerings.presents.client.InvocationService;
import com.threerings.presents.data.InvocationCodes;

/**
 * Simplifies a common pattern which is to post an {@link Invoker} unit which does some database
 * operation and then calls back to an {@link InvocationService.InvocationListener} of some
 * sort. If the database operation fails, the error will be logged and the result listener will be
 * replied to with {@link InvocationCodes#INTERNAL_ERROR}.
 */
public abstract class PersistingUnit extends Invoker.Unit
{
    public PersistingUnit (InvocationService.InvocationListener listener)
    {
        this("UnknownPersistingUnit", listener);
    }

    public PersistingUnit (
        String name, InvocationService.InvocationListener listener)
    {
        super(name);
        _listener = listener;
    }

    /**
     * This method is where the unit performs its persistent actions. Any persistence exception
     * will be caught and logged along with the output from {@link #getFailureMessage}, if any.
     */
    public abstract void invokePersistent ()
        throws PersistenceException;

    /**
     * Handles the success case, which by default is to do nothing.
     */
    public void handleSuccess ()
    {
    }

    /**
     * Handles the failure case by logging the error and reporting an internal error to the
     * listener.
     */
    public void handleFailure (PersistenceException error)
    {
        Log.warning(getFailureMessage());
        Log.logStackTrace(error);
        _listener.requestFailed(InvocationCodes.INTERNAL_ERROR);
    }

    /**
     * Provides a custom failure message in the event that the persistent action fails. This will
     * be logged along with the exception.
     */
    public String getFailureMessage ()
    {
        return this + " failed.";
    }

    public boolean invoke ()
    {
        try {
            invokePersistent();
        } catch (PersistenceException pe) {
            _error = pe;
        }
        return true;
    }

    public void handleResult ()
    {
        if (_error != null) {
            handleFailure(_error);
        } else {
            handleSuccess();
        }
    }

    protected InvocationService.InvocationListener _listener;
    protected PersistenceException _error;
}<commit_msg>getFailureMessage() should be protected as it's not part of the persisting unit's public API.<commit_after>//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2007 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.presents.util;

import com.samskivert.io.PersistenceException;
import com.samskivert.util.Invoker;

import com.threerings.presents.Log;
import com.threerings.presents.client.InvocationService;
import com.threerings.presents.data.InvocationCodes;

/**
 * Simplifies a common pattern which is to post an {@link Invoker} unit which does some database
 * operation and then calls back to an {@link InvocationService.InvocationListener} of some
 * sort. If the database operation fails, the error will be logged and the result listener will be
 * replied to with {@link InvocationCodes#INTERNAL_ERROR}.
 */
public abstract class PersistingUnit extends Invoker.Unit
{
    public PersistingUnit (InvocationService.InvocationListener listener)
    {
        this("UnknownPersistingUnit", listener);
    }

    public PersistingUnit (String name, InvocationService.InvocationListener listener)
    {
        super(name);
        _listener = listener;
    }

    /**
     * This method is where the unit performs its persistent actions. Any persistence exception
     * will be caught and logged along with the output from {@link #getFailureMessage}, if any.
     */
    public abstract void invokePersistent ()
        throws PersistenceException;

    /**
     * Handles the success case, which by default is to do nothing.
     */
    public void handleSuccess ()
    {
    }

    /**
     * Handles the failure case by logging the error and reporting an internal error to the
     * listener.
     */
    public void handleFailure (PersistenceException error)
    {
        Log.warning(getFailureMessage());
        Log.logStackTrace(error);
        _listener.requestFailed(InvocationCodes.INTERNAL_ERROR);
    }

    @Override // from Invoker.Unit
    public boolean invoke ()
    {
        try {
            invokePersistent();
        } catch (PersistenceException pe) {
            _error = pe;
        }
        return true;
    }

    @Override // from Invoker.Unit
    public void handleResult ()
    {
        if (_error != null) {
            handleFailure(_error);
        } else {
            handleSuccess();
        }
    }

    /**
     * Provides a custom failure message in the event that the persistent action fails. This will
     * be logged along with the exception.
     */
    protected String getFailureMessage ()
    {
        return this + " failed.";
    }

    protected InvocationService.InvocationListener _listener;
    protected PersistenceException _error;
}
{'fraction_non_alphanumeric': 0.0786662817551963, 'fraction_numerical': 0.006928406466512702, 'mean_word_length': 2.8580178173719375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1459700', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1760, 'n_words': 705}