com.centretown.parts.server.domain.Membership.java Source code

Java tutorial

Introduction

Here is the source code for com.centretown.parts.server.domain.Membership.java

Source

/**
 * Copyright 2014 Centretown Software (Dave Marsh)
 *
 * Licensed 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 com.centretown.parts.server.domain;

import java.util.Collection;
import java.util.Map;
import java.util.SortedMap;

import com.centretown.parts.shared.dto.MemberDto;
import com.centretown.parts.shared.dto.MemberKey;
import com.centretown.parts.shared.dto.MembershipDto;
import com.google.common.collect.Maps;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Ignore;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Mapify;
import com.googlecode.objectify.annotation.OnLoad;
import com.googlecode.objectify.condition.IfNotNull;
import com.googlecode.objectify.mapper.Mapper;

/**
 * represents the membership of a group - detached from group to assist quick
 * loading of groups for lists - map access by member id (user id)
 * 
 * @author Dave
 *
 */
@Entity
public class Membership extends AbstractBaseEntity {
    /**
     * 
     * @param source
     *            a Membership record
     * @return a MembershipDto pojo
     */
    public static MembershipDto createDto(Membership source) {
        MembershipDto target = new MembershipDto();
        target.copyBase(source);
        target.setGroupId(source.groupId);
        target.setMap(source.ids);
        return target;
    }

    /**
     * 
     * @param source
     *            a MembershipDto pojo
     * @return a Membership record
     */
    public static Membership create(MembershipDto source) {
        Membership target = new Membership();
        target.copyBase(source);
        target.groupId = source.getGroupId();
        target.ids = source.getMap();
        return target;
    }

    @Index(IfNotNull.class)
    private Long groupId;

    @Ignore
    private Group group;

    public static class MemberMapper implements Mapper<MemberKey, MemberDto> {
        @Override
        public MemberKey getKey(MemberDto value) {
            return value;
        }
    }

    @Mapify(MemberMapper.class)
    private SortedMap<MemberKey, MemberDto> names;

    @Ignore
    private Map<Long, MemberDto> ids;

    /**
     * default constructor (protected)
     */
    protected Membership() {
        super();
        this.names = Maps.newTreeMap();
        this.ids = Maps.newHashMap();
    }

    @OnLoad
    protected void createIdMap() {
        for (MemberDto member : names.values()) {
            ids.put(member.getId(), member);
        }
    }

    /**
     * public constructor requires group
     * 
     * @param group
     */
    public Membership(Group group) {
        this();
        this.groupId = group.getId();
    }

    public SortedMap<MemberKey, MemberDto> getMap() {
        return names;
    }

    /**
     * 
     * @return the membership group id
     */
    public Long getGroupId() {
        return groupId;
    }

    /**
     * 
     * @return the number of members
     */
    public int size() {
        return names.size();
    }

    /**
     * 
     * @param user
     *            the user to add/update
     * @param isPublic
     *            display publicly?
     * @param roles
     *            roles in this group
     * @return the updated member
     */
    public MemberDto put(User user, Boolean isPublic, String... roles) {
        return put(new Member(user, isPublic, roles));
    }

    /**
     * 
     * @param member
     *            the user to add/update
     * @return the updated member
     */
    public MemberDto put(Member member) {
        setDirty(true);
        MemberDto currentMember = ids.get(member.getId());
        if (currentMember != null && currentMember.compareTo(member) != 0) {
            // member name change
            names.remove(currentMember);
        }
        ids.put(member.getId(), member);
        return names.put(member, member);
    }

    /**
     * 
     * @param user
     *            the user to remove
     * @return the removed member
     */
    public MemberDto remove(User user) {
        return remove(user.getId());
    }

    /**
     * 
     * @param member
     *            the member to remove
     * @return the removed member
     */
    public MemberDto remove(Member member) {
        return remove(member.getId());
    }

    /**
     * 
     * @param id
     *            the member to remove
     * @return the removed member
     */
    public MemberDto remove(Long id) {
        setDirty(true);
        MemberDto member = ids.remove(id);
        return names.remove(member);
    }

    /**
     * 
     * @param users
     *            the collection of users to add/update
     * @param isPublic
     *            default privacy setting
     * @param roles
     *            default membership roles
     */
    public void putAll(Collection<User> users, Boolean isPublic, String... roles) {
        setDirty(true);
        for (User user : users) {
            Member member = new Member(user, isPublic, roles);
            names.put(member, member);
            ids.put(member.getId(), member);
        }
    }
}