com.couchbase.trombi.services.SearchService.java Source code

Java tutorial

Introduction

Here is the source code for com.couchbase.trombi.services.SearchService.java

Source

/*
 * Copyright (c) 2016 Couchbase, Inc.
 *
 * 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.couchbase.trombi.services;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.couchbase.trombi.data.CoworkerRepository;
import com.couchbase.trombi.domain.Coworker;
import com.couchbase.trombi.domain.Location;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.stereotype.Service;

@Service
public class SearchService {

    private final CoworkerRepository coworkerRepository;

    @Autowired
    public SearchService(CoworkerRepository coworkerRepository) {
        this.coworkerRepository = coworkerRepository;
    }

    public List<Coworker> findCoworkersNear(String imHandle) {
        List<Coworker> lc = coworkerRepository.findAllByIm(imHandle);
        if (lc == null || lc.isEmpty()) {
            return Collections.emptyList();
        }
        Coworker c = lc.get(0);

        Location latest = c.getLastCheckin();
        if (latest == null) {
            latest = c.getMainLocation();
        }

        return coworkerRepository.findAllByMainLocationCoordinatesNear(latest.getCoordinates(),
                new Distance(10, Metrics.KILOMETERS));
    }

    public List<Coworker> findNearestTeamMembers(String teamName, String coworkerId) {
        List<Coworker> lc;
        if (teamName.equals("*"))
            lc = (List<Coworker>) coworkerRepository.findAll();
        else
            lc = coworkerRepository.findByTeamIs(teamName);
        Coworker center = coworkerRepository.findOne(coworkerId);
        if (lc == null || lc.isEmpty() || center == null) {
            return Collections.emptyList();
        }

        final int centerZoneOffset = center.getLastCheckin() == null ? center.getMainLocation().getTimeZoneOffset()
                : center.getLastCheckin().getTimeZoneOffset();

        Comparator<Coworker> zoneComparator = new Comparator<Coworker>() {
            @Override
            public int compare(Coworker o1, Coworker o2) {
                int offset1 = o1.getLastCheckin() == null ? o1.getMainLocation().getTimeZoneOffset()
                        : o1.getLastCheckin().getTimeZoneOffset();
                int offset2 = o2.getLastCheckin() == null ? o2.getMainLocation().getTimeZoneOffset()
                        : o2.getLastCheckin().getTimeZoneOffset();

                int hourDiff1 = Math.abs(offset1 - centerZoneOffset);
                int hourDiff2 = Math.abs(offset2 - centerZoneOffset);

                return hourDiff1 - hourDiff2;
            }
        };

        Collections.sort(lc, zoneComparator);
        return lc;
    }
}