ronan ronan - 3 months ago 7
Java Question

Code Refactoring Extract To a Method

Friends

I have below two overloaded methods which throw same ServiceException

private ModResponse updateNDelTerms(GlobalTermDeleteType item, boolean isGlobal)
{
StdTerm stdTerm = getTerm(item.getstdTermId());
if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
{
throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
}

if (stdTerm.isGlobal() && !isGlobal)
{
throw new ServiceException(
"Global Term can not be updated: incorrect URL.",
CANNOT_UPDATE_GLOBAL_TERM, INCORRECT_URL);
}

if (stdTerm.isLocked() != null && stdTerm.isLocked())
{
throw new ServiceException("Global Term can not be updated: stdTerm is locked.",
CANNOT_UPDATE_GLOBAL_TERM, TERM_LOCKED);
}

return updateNDel(item, stdTerm);
}


Second method is

public ItemResponse<List<stdTermItemType>> copyTerm(
BigInteger stdTermId, boolean isGlobal,
boolean isFalse)
{
StdTerm stdTerm = getTerm(stdTermId);

if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
{
throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
}

if (stdTerm.isGlobal() && !isGlobal)
{
throw new ServiceException(
"Global Term can not be updated: incorrect URL.",
CANNOT_COPY_GLOBAL_TERM, INCORRECT_URL);
}

if (stdTerm.isLocked() != null && stdTerm.isLocked())
{
throw new ServiceException("Rate sheet term can not be updated: stdTerm is locked.",
CANNOT_COPY_GLOBAL_TERM, TERM_LOCKED);
}
return copyGlobleTerm(stdTerm, pasteTermObj, isFalse);
}


I am thinking of refactoring these two methods and use Extract common code to a method ,
However due to difference in CANNOT_COPY_GLOBAL_TERM and CANNOT_UPDATE_GLOBAL_TERM which are unique to both the methods hence
Im not able to achieve Extract to a Method refactoring .
Please suggest .

Answer
private StdTerm retrieveStdTerm(BigInteger stdTermId, boolean isGlobal, String errorTerm)
{
    StdTerm stdTerm = getTerm(BigInteger stdTermId);
    if (stdTerm == null || !getBuilder().getOwnerId().equals(stdTerm.getOwnerId()))
    {
        throw new ServiceException(GLOBAL_TERM_TO_DELETE_DOES_NOT_EXIST_MSG,
                CANNOT_DELETE_GLOBAL_TERM, GLOBAL_TERM_DOES_NOT_EXIST);
    }
    if (stdTerm.isGlobal() && !isGlobal)
    {
        throw new ServiceException(
                "Global Term can not be updated: incorrect URL.",
                errorTerm, INCORRECT_URL);
    }
    if (stdTerm.isLocked() != null && stdTerm.isLocked())
    {
        throw new ServiceException("Global Term can not be updated: stdTerm is locked.",
                errorTerm, TERM_LOCKED);
    }
    return stdTerm;
}

private ModResponse updateNDelTerms(GlobalTermDeleteType item, boolean isGlobal)
{
    StdTerm stdTerm = retrieveStdTerm(item.getstdTermId(), isGlobal,
            CANNOT_DELETE_GLOBAL_TERM);
    return updateNDel(item, stdTerm);
}

public ItemResponse<List<stdTermItemType>> copyTerm(BigInteger stdTermId, boolean isGlobal,
        boolean isFalse)
{
    StdTerm stdTerm = retrieveStdTerm(stdTermId, isGlobal, CANNOT_COPY_GLOBAL_TERM);
    return copyGlobleTerm(stdTerm, pasteTermObj, isFalse);
}
Comments