Which approach is better and why?
First approach (inner class for wrapping):
public class OOP {
private SomeService someService;
private final class OrganizationPrimaryKeyString implements Serializable {
private static final long serialVersionUID = -256615521081939636L;
private final String pkString
public OrganizationPrimaryKeyString(final OrganizationEnum organizationEnum) {
final PK pk = someService.getValue(organizationEnum).getPk();
if (pk == null) {
throw new IllegalArgumentException("Can't be null");
}
pkString = pk.getLongValueAsString();
}
@Override
public String toString() {
return pkString;
}
}
public List<Information> getInformation(final OrganizationEnum organizationEnum) {
final OrganizationPrimaryKeyString organization = new OrganizationPrimaryKeyString(organizationEnum);
try {
final SessionContext ctx = createAndSetupLocalSessionContext(organization);
(...)
return (...);
} catch (final FactoryException e) {
throw new SystemException(e.getMessage(), e);
} finally {
closeLocalSessionContext();
}
}
private SessionContext createAndSetupLocalSessionContext(final OrganizationPrimaryKeyString organization) {
(...)
sessionContext.setAttribute("organization", salesOrganization.toString());
return sessionContext;
}
}
Second approach (class method):
public class OOP {
private SomeService someService;
public List<Information> getInformation(final OrganizationEnum organizationEnum) {
try {
final SessionContext ctx = createAndSetupLocalSessionContext(organizationEnum);
(...)
return (...);
} catch (final FactoryException e) {
throw new SystemException(e.getMessage(), e);
} finally {
closeLocalSessionContext();
}
}
private SessionContext createAndSetupLocalSessionContext(final OrganizationEnum organizationEnum) {
(...)
sessionContext.setAttribute("organization", getPkString(organizationEnum));
return sessionContext;
}
private String getPkString(final OrganizationEnum organizationEnum) {
final PK pk = someService.getValue(organizationEnum).getPk();
if (pk == null) {
throw new IllegalArgumentException("Can't be null");
}
return pk.getLongValueAsString();
}
}