/*
 * Copyright 2019, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.server.groups;

import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Timestamp;
import io.spine.core.EventContext;
import io.spine.core.Subscribe;
import io.spine.server.organizations.Organization;
import io.spine.server.projection.Projection;
import io.spine.server.projection.ProjectionRepository;
import io.spine.server.route.StateUpdateRouting;

public final class GroupProjection extends Projection<GroupId, Group, GroupVBuilder> {

    private GroupProjection(GroupId id) {
        super(id);
    }

    @Subscribe(external = true)
    void on(Organization organization, EventContext systemContext) {
        Timestamp updateTime = systemContext.getTimestamp();
        builder().setId(id())
                 .setName(organization.getName() + updateTime)
                 .addAllParticipants(organization.getMembersList())
                 .addParticipants(organization.getHead());
    }

    public static final class Repository
            extends ProjectionRepository<GroupId, GroupProjection, Group> {

        @Override
        public void onRegistered() {
            super.onRegistered();
            StateUpdateRouting<GroupId> routing = StateUpdateRouting.newInstance();
            routing.route(Organization.class, (org, eventContext) ->
                    ImmutableSet.of(GroupId.newBuilder()
                                           .setUuid(org.getHead()
                                                       .getValue())
                                           .build()));
            eventRouting().routeEntityStateUpdates(routing);
        }
    }
}

@@ -1043,55 +1043,8 @@
 s;%0A%0A
-import com.google.common.collect.ImmutableSet;%0A
 impo
@@ -1347,16 +1347,72 @@
 uting;%0A%0A
+import static io.spine.server.route.EventRoute.withId;%0A%0A
 public f
@@ -1495,76 +1495,8 @@
  %7B%0A%0A
-    private GroupProjection(GroupId id) %7B%0A        super(id);%0A    %7D%0A%0A
     
@@ -2264,23 +2264,14 @@
     
-ImmutableSet.of
+withId
 (Gro
@@ -2288,25 +2288,16 @@
 ilder()%0A
-         
         
@@ -2391,25 +2391,16 @@
         
-         
 .getValu
@@ -2400,33 +2400,24 @@
 getValue())%0A
-         
             

Remove redundant constructor