public static void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) { String[] split = split2(message.body); if (split.length < 2) { throw new IllegalCommandException("Wrong income message format."); } int dashId = Integer.parseInt(split[0]); String deviceString = split[1]; if (deviceString == null || deviceString.isEmpty()) { throw new IllegalCommandException("Income tag message is empty."); } Profile profile = user.profile; DashBoard dash = profile.getDashByIdOrThrow(dashId); Tag newTag = JsonParser.parseTag(deviceString, message.id); log.debug("Creating new tag {}.", newTag); if (newTag.isNotValid()) { throw new IllegalCommandException("Income tag name is not valid."); } for (Tag tag : dash.tags) { if (tag.id == newTag.id || tag.name.equals(newTag.name)) { throw new IllegalCommandException("Tag with same id/name already exists."); } } profile.addTag(dash, newTag); user.lastModifiedTs = System.currentTimeMillis(); if (ctx.channel().isWritable()) { ctx.writeAndFlush(makeUTF8StringMessage(CREATE_TAG, message.id, newTag.toString()), ctx.voidPromise()); } }
public static void messageReceived(ChannelHandlerContext ctx, User user, StringMessage message) { String[] split = split2(message.body); if (split.length < 2) { throw new IllegalCommandException("Wrong income message format."); } int dashId = Integer.parseInt(split[0]); String tagString = split[1]; if (tagString == null || tagString.isEmpty()) { throw new IllegalCommandException("Income tag message is empty."); } DashBoard dash = user.profile.getDashByIdOrThrow(dashId); Tag newTag = JsonParser.parseTag(tagString, message.id); log.debug("Updating new tag {}.", tagString); if (newTag.isNotValid()) { throw new IllegalCommandException("Income tag name is not valid."); } Tag existingTag = user.profile.getTagById(dash, newTag.id); if (existingTag == null) { throw new IllegalCommandException("Attempt to update tag with non existing id."); } existingTag.update(newTag); user.lastModifiedTs = System.currentTimeMillis(); ctx.writeAndFlush(ok(message.id), ctx.voidPromise()); }