mardi 30 juin 2015

Java: Updating Data Source through a Row Data Gateway

I have a member application which keeps tracks of members of a club (Name, ID). Currently I can add and I can delete but the problem is Updating. I have a hard time wrapping my head around accomplishing this with Java in the existing architecture (see below).

For adding, I have an ActionListener that will call CommandAddMember using the Name from the TextField.

memberNameButton.addActionListener(new ActionListener()
{
    @Override
    public void actionPerformed(ActionEvent e) {
    CommandAddMember command = new
    CommandAddMember(addMemberNameTextField.getText());
    ModelFacade.getSingleton().queueCommand(command);
    }
});

The CommandMember looks like this:

public class CommandAddMember extends Command
{
    String memberName;
    public CommandAddMember(String name) {
    memberName = name;
    }

    @Override
    protected boolean execute() throws DatabaseException
    {
    MemberRowDataGatewayMock gateway = null;
    gateway = new MemberRowDataGatewayMock(memberName);
    int memberId = gateway.getMemberID();
    MemberResponseReport report = new
    MemberResponseReport(memberId, memberName, new ArrayList<String>());       
    QualifiedObservableConnector.getSingleton().sendReport(report);
    return true;
    }   
}

So here we access the MemberRowDataGatewayMock. The memberId is populated from gateway.getMemberID(); which uses a Hashmap and nextKey++ to increment by one (my guess is by whichever is last, like a real database sequence would). How can I simply update the existing MemberName while leaving the ID untouched? in SQL terms this would be

UPDATE table SET MemberName = 'NewName' where ID = 12;

For reference, here is the MemberRowDataGatewayMock:

public class MemberRowDataGatewayMock implements MemberRowDataGateway
{

    private class MemberInfo
    {
        private String memberName;

        public MemberInfo(String memberName)
        {
            this.memberName = memberName;
        }

    }


    private static HashMap<Integer, MemberInfo> memberInfo;
    private static int nextKey = 1;
    private int memberID;
    private MemberInfo info;

    /**
     * Finder constructor - will initialize itself from the stored information
     * 
     * @param memberID
     *            the ID of the member we are looking for
     * @throws DatabaseException
     *             if the memberID isn't in the data source
     */
    public MemberRowDataGatewayMock(int memberID) throws DatabaseException
    {
        if (memberInfo == null)
        {
            resetData();
        }

        if (memberInfo.containsKey(memberID))
        {
            info = memberInfo.get(memberID);
            this.memberID = memberID;
        } else
        {
            throw new DatabaseException("Couldn't find member with ID "
                    + memberID);
        }
    }

    public MemberRowDataGatewayMock(String memberName)
    {
        if (memberInfo == null)
        {
            resetData();
        }
        memberID = nextKey;
        nextKey++;

        info = new MemberInfo(memberName);
        memberInfo.put(memberID, info);
    }

    /**
     * Just used by tests to reset static information
     */
    public MemberRowDataGatewayMock()
    {
    }

    /**
     * @see datasource.MemberRowDataGateway#resetData()
     */
    @Override
    public void resetData()
    {
        memberInfo = new HashMap<Integer, MemberInfo>();
        nextKey = 1;
        for (MembersForTest p : MembersForTest.values())
        {
            memberInfo.put(nextKey, new MemberInfo(p.getMemberName()));
            nextKey++;
        }
    }

    /**
     * @throws DatabaseException 
     * @see datasource.MemberRowDataGateway#getMemberID()
     */
    @Override
    public int getMemberID() throws DatabaseException
    {
        if (info == null) {
            throw new DatabaseException("Member has already been deleted.");
        }
        return memberID;
    }

    /**
     * @see datasource.MemberRowDataGateway#persist()
     */
    @Override
    public void persist()
    {
        memberInfo.put(memberID, info);
    }

    /**
     * @throws DatabaseException 
     * @see datasource.MemberRowDataGateway#getMemberName()
     */
    @Override
    public String getMemberName() throws DatabaseException
    {
        if (info == null) {
            throw new DatabaseException("Member has already been deleted.");
        }
        return info.memberName;
    }

    @Override
    public String addMemberName()
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void deleteMember()
    {
        memberInfo.remove(memberID);
        info = null;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((info == null) ? 0 : info.hashCode());
        result = prime * result + memberID;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MemberRowDataGatewayMock other = (MemberRowDataGatewayMock) obj;
        if (info == null) {
            if (other.info != null)
                return false;
        } else if (!info.equals(other.info))
            return false;
        if (memberID != other.memberID)
            return false;
        return true;
    }

}

Aucun commentaire:

Enregistrer un commentaire