Just from where this took place, I can see the deputy being ignorant of the law, but as it went up the rungs of the ladder, it should have been stopped. First, the Chief of Police, should have released the kid, and thrown it out, next the DA should have not taken it, and told the chief they had no case, but then it gets worse and is brought before a judge, who lets it continue, and didn't tell the DA he had no case. It is one taking up for another, and another, in that the government never does any wrong.
It seems to me, that each one involved, needs to sit through a course on civilian rights, and their rights under the US Constitution, before they are allowed to work within the government again. Just wait until election, when all those gun owners go vote.