-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/com/savvato/tribeapp/services/SearchServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,70 @@ | ||
package com.savvato.tribeapp.services; | ||
|
||
import com.savvato.tribeapp.dto.AttributeDTO; | ||
import com.savvato.tribeapp.dto.ConnectOutgoingMessageDTO; | ||
import com.savvato.tribeapp.dto.SearchResponseDTO; | ||
import org.aspectj.weaver.ast.Literal; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class SearchServiceImpl implements SearchService { | ||
|
||
@Autowired ConnectService connectService; | ||
|
||
@Autowired AttributesService attributesService; | ||
|
||
@Override | ||
public Optional<SearchResponseDTO> search(Long requestingUserId, List<String> phrases) { | ||
|
||
// get all connections which either are, or have a connection to another user which has, the given phrases | ||
// return the connections in a SearchResponseDTO | ||
|
||
// first get all connections as a requesting user, | ||
List<ConnectOutgoingMessageDTO> requestingUserConnections = connectService.getConnectionsWhereUserWasTheRequestingUser(requestingUserId); | ||
|
||
depthFirstSearch(requestingUserConnections, requestingUserId, phrases, 0); | ||
|
||
// do a depth first search to find all connections that have the given phrases | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
final int MAX_DEPTH = 4; | ||
private boolean depthFirstSearch(List<ConnectOutgoingMessageDTO> connections, Long userId, List<String> phrases, int depth) { | ||
// do a depth first search to find all connections that have the given phrases | ||
|
||
Iterator<ConnectOutgoingMessageDTO> iterator = connections.iterator(); | ||
|
||
while (iterator.hasNext()) { | ||
ConnectOutgoingMessageDTO connection = iterator.next(); | ||
if (depth < MAX_DEPTH) { | ||
// get requested connections for this user | ||
List<ConnectOutgoingMessageDTO> connectionsForUser = connectService.getConnectionsWhereUserWasTheRequestingUser(connection.to.userId); | ||
depthFirstSearch(connectionsForUser, connection.to.userId, phrases, depth + 1); | ||
} | ||
} | ||
|
||
// no more connections, so check this connection for the phrases | ||
Optional<List<AttributeDTO>> attributesByUserId = attributesService.getAttributesByUserId(userId); | ||
if (attributesByUserId.isPresent()) { | ||
List<AttributeDTO> attributes = attributesByUserId.get(); | ||
for (String phrase : phrases) { | ||
for (AttributeDTO attribute : attributes) { | ||
if (attribute.phrase.equals(phrase)) { | ||
// found a match | ||
return true; | ||
|
||
// WILO: If a match is found we should return the path to it. | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |