// Path: src/main/java/org/swarmcom/jsynapse/dao/RoomAliasRepository.java
// public interface RoomAliasRepository extends MongoRepository<RoomAlias, String> {
// 
//     RoomAlias findByAliasAndServer(String alias, String server);
// 
//     List<RoomAlias> findByAlias(String alias);
// 
//     List<RoomAlias> findByRoomId(String roomId);
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/domain/RoomAlias.java
// public class RoomAlias {
//     private static final String PREFIX = "#";
//     private static final String SEPARATOR = ":";
// 
//     @Id
//     String id;
// 
//     @Indexed
//     @JsonProperty("room_id")
//     String roomId;
// 
//     String alias;
// 
//     String server;
// 
//     public RoomAlias(String roomId, String alias, String server) {
//         this.roomId = roomId;
//         this.alias = alias;
//         this.server = server;
//     }
// 
//     public String getRoomId() {
//         return roomId;
//     }
// 
//     public String getAlias() {
//         return alias;
//     }
// 
//     public String getServer() {
//         return server;
//     }
// 
//     public static class RoomAliases {
//         @JsonProperty
//         List<String> aliases = new LinkedList<>();
// 
//         public RoomAliases(List<RoomAlias> roomAliases) {
//             for (RoomAlias roomAlias : roomAliases) {
//                 aliases.add(StringUtils.join(PREFIX, roomAlias.getAlias(), SEPARATOR, roomAlias.getServer()));
//             }
//         }
// 
//         public List<String> getAliases() {
//             return aliases;
//         }
//     }
// 
//     public static class AliasServers {
//         @JsonProperty("room_id")
//         String roomId;
// 
//         @JsonProperty
//         List<String> servers = new LinkedList<>();
// 
//         public AliasServers(String roomId, List<RoomAlias> roomAliases) {
//             this.roomId = roomId;
//             for (RoomAlias roomAlias : roomAliases) {
//                 servers.add(roomAlias.getServer());
//             }
//         }
// 
//         public String getRoomId() {
//             return roomId;
//         }
// 
//         public List<String> getServers() {
//             return servers;
//         }
//     }
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/service/exception/EntityAlreadyExistsException.java
// public class EntityAlreadyExistsException extends RuntimeException {
//     public EntityAlreadyExistsException(final String message) {
//         super(message);
//     }
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/JSynapseServer.java
// public static String DOMAIN = "swarmcom.org";


import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.swarmcom.jsynapse.dao.RoomAliasRepository;
import org.swarmcom.jsynapse.domain.RoomAlias;
import org.swarmcom.jsynapse.service.exception.EntityAlreadyExistsException;
import javax.inject.Inject;
import java.util.List;
import java.util.regex.Pattern;
import static org.swarmcom.jsynapse.JSynapseServer.DOMAIN;
/*
 * (C) Copyright 2015 eZuce 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 org.swarmcom.jsynapse.service.room;





@Service
@Validated
public class RoomAliasServiceImpl implements RoomAliasService {
    private RoomAliasRepository roomAliasRepository;

    @Inject
    public RoomAliasServiceImpl(final RoomAliasRepository rAliasRepository) {
        this.roomAliasRepository = rAliasRepository;
    }

    public RoomAlias createAlias(String roomId, String alias) {
        RoomAlias roomAlias = new RoomAlias(roomId, alias, DOMAIN);
        RoomAlias existingRoomAlias = roomAliasRepository.findByAliasAndServer(alias, DOMAIN);
        if (existingRoomAlias == null) {
            return roomAliasRepository.save(roomAlias);
        } else {
            throw new EntityAlreadyExistsException("Room alias already taken");
        }
    }

    public void deleteAlias(String alias) {
// Path: src/main/java/org/swarmcom/jsynapse/dao/RoomAliasRepository.java
// public interface RoomAliasRepository extends MongoRepository<RoomAlias, String> {
// 
//     RoomAlias findByAliasAndServer(String alias, String server);
// 
//     List<RoomAlias> findByAlias(String alias);
// 
//     List<RoomAlias> findByRoomId(String roomId);
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/domain/RoomAlias.java
// public class RoomAlias {
//     private static final String PREFIX = "#";
//     private static final String SEPARATOR = ":";
// 
//     @Id
//     String id;
// 
//     @Indexed
//     @JsonProperty("room_id")
//     String roomId;
// 
//     String alias;
// 
//     String server;
// 
//     public RoomAlias(String roomId, String alias, String server) {
//         this.roomId = roomId;
//         this.alias = alias;
//         this.server = server;
//     }
// 
//     public String getRoomId() {
//         return roomId;
//     }
// 
//     public String getAlias() {
//         return alias;
//     }
// 
//     public String getServer() {
//         return server;
//     }
// 
//     public static class RoomAliases {
//         @JsonProperty
//         List<String> aliases = new LinkedList<>();
// 
//         public RoomAliases(List<RoomAlias> roomAliases) {
//             for (RoomAlias roomAlias : roomAliases) {
//                 aliases.add(StringUtils.join(PREFIX, roomAlias.getAlias(), SEPARATOR, roomAlias.getServer()));
//             }
//         }
// 
//         public List<String> getAliases() {
//             return aliases;
//         }
//     }
// 
//     public static class AliasServers {
//         @JsonProperty("room_id")
//         String roomId;
// 
//         @JsonProperty
//         List<String> servers = new LinkedList<>();
// 
//         public AliasServers(String roomId, List<RoomAlias> roomAliases) {
//             this.roomId = roomId;
//             for (RoomAlias roomAlias : roomAliases) {
//                 servers.add(roomAlias.getServer());
//             }
//         }
// 
//         public String getRoomId() {
//             return roomId;
//         }
// 
//         public List<String> getServers() {
//             return servers;
//         }
//     }
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/service/exception/EntityAlreadyExistsException.java
// public class EntityAlreadyExistsException extends RuntimeException {
//     public EntityAlreadyExistsException(final String message) {
//         super(message);
//     }
// }
// 
// Path: src/main/java/org/swarmcom/jsynapse/JSynapseServer.java
// public static String DOMAIN = "swarmcom.org";


// Path: src/main/java/org/swarmcom/jsynapse/service/room/RoomAliasServiceImpl.java
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.swarmcom.jsynapse.dao.RoomAliasRepository;
import org.swarmcom.jsynapse.domain.RoomAlias;
import org.swarmcom.jsynapse.service.exception.EntityAlreadyExistsException;
import javax.inject.Inject;
import java.util.List;
import java.util.regex.Pattern;
import static org.swarmcom.jsynapse.JSynapseServer.DOMAIN;

/*
 * (C) Copyright 2015 eZuce 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 org.swarmcom.jsynapse.service.room;





@Service
@Validated
public class RoomAliasServiceImpl implements RoomAliasService {
    private RoomAliasRepository roomAliasRepository;

    @Inject
    public RoomAliasServiceImpl(final RoomAliasRepository rAliasRepository) {
        this.roomAliasRepository = rAliasRepository;
    }

    public RoomAlias createAlias(String roomId, String alias) {
        RoomAlias roomAlias = new RoomAlias(roomId, alias, DOMAIN);
        RoomAlias existingRoomAlias = roomAliasRepository.findByAliasAndServer(alias, DOMAIN);
        if (existingRoomAlias == null) {
            return roomAliasRepository.save(roomAlias);
        } else {
            throw new EntityAlreadyExistsException("Room alias already taken");
        }
    }

    public void deleteAlias(String alias) {
        RoomAlias roomAlias = roomAliasRepository.findByAliasAndServer(alias, DOMAIN);